Showing the image with a user interface
Now, we need to make a new view and method from the controller to show the information of the image uploaded. This can be done as follows:
- First, we need to define a
GET
route for the controller. For this, open your fileroutes.php
in theapp
folder and add the following codes://This is to show the image's permalink on our website Route::get('snatch/{id}', array('as'=>'get_image_information', 'uses'=>'[email protected]')) ->where('id', '[0-9]+');
We defined an
id
variable on the route, and with thewhere()
method, using regular expression, we filtered it first hand. So we don’t need to worry about filtering the ID field, whether it’s a natural number or not. - Now, let’s create our controller method. Add the following code inside
imageController.php
inapp/http/controllers/
before the last curly bracket (}
):public function getSnatch($id) { //Let's try to find the image from database first $image = image::find($id); //If found, we load the view and pass the image info asparameter, else we redirect to main page with errormessage if($image) { return View::make('tpl.permalink')->with('image',$image); } else { return Redirect::to('/')->with('error','image not found'); } }
First, we looked for the image with the
find()
method of Eloquent ORM. If it returns the value as false, that means there is a row found. So we can simply check whether there is a result or not with a simpleif
clause. If there is a result, we will load our view with the found image info as a variable named$image
, using thewith()
method. If no values are found, we return to the index page with an error message. - Now let’s create the template file containing the following code. Save this file as
permalink.blade.php
in resources/views/tpl/
:@extends('frontend_master') @section('content') <table cellpadding="0" cellspacing="0" border="0"width="100percent"> <tr> <td width="450" valign="top"> <p>Title: {{$image->title}}</p> {{HTML::image(Config::get('image.thumb_folder').'/'.$image->image)}} </td> <td valign="top"> <p>Direct image URL</p> <input onclick="this.select()" type="text"width="100percent" value="{{URL::to(Config::get('image.upload_folder').'/'$image->image)}}" /> <p>Thumbnail Forum BBCode</p> <input onclick="this.select()" type="text"width="100percent" value="[url={{URL::to('snatch/'$image->id)}}][img]{{URL::to(Config::get('image.thumb_folder')'/'.$image->image)}}[/img][/url]" /> <p>Thumbnail HTML Code</p> <input onclick="this.select()" type="text"width="100percent"value="{{HTML::entities(HTML::link(URL::to('snatch/'.$image->id),HTML::image(Config::get('image.thumb_folder').'/'$image->image)))}}" /> </td> </tr> </table> @stop
You should be familiar with most methods used in this template by now. There is a new method called
entities()
of theHTML
class, which actually ishtmlentities()
of raw PHP, but with some pre-checks and as Laravel’s way.Also, because we’ve returned the
$image
variable to the view (which is the database row object that we’ve gained using Eloquent), we can use it directly as$image->columnName
in the view.We have added a permalink feature for our project, but what if we want to show all the images? For that, we need an
'all pages'
section in our system.
Comments