Typically, a route function returns a View. Views consist of plain ole' HTML and are stored in your application/views directory.
A very simple view file could look like this:
<html>
Welcome to my website!
</html>
Assuming this view lives in application/views/simple.php, we could return it from a route like so:
'GET /home' => function()
{
return View::make('simple');
}
When building a large application, you may want to organize your views into sub-directories. That's a great idea! Assuming a view lives in application/views/user/login.php, we could return it like so:
'GET /home' => function()
{
return View::make('user/login');
}
It's that simple. Of course, you are not required to return a View. Strings are also welcome:
'GET /home' => function()
{
return json_encode('This is my response!');
}
You can pass data to a view by "binding" the data to a variable. This is done using the bind method on the View:
'GET /home' => function()
{
return View::make('simple')->bind('email', 'example@gmail.com');
}
In the example above, the first parameter is the name of the view variable. The second parameter is the value that will be assigned to the variable.
Now, in our view, we can access the e-mail address like so:
<html>
<?php echo $email; ?>
</html>
Of course, we can bind as many variables as we wish:
'GET /home' => function()
{
return View::make('simple')
->bind('name', 'Taylor')
->bind('email', 'example@gmail.com');
}
You may also bind view data by simply setting properties on a view instance:
'GET /home' => function()
{
$view = View::make('user/login');
$view->name = 'Taylor';
$view->email = 'example@gmail.com';
return $view;
}
Want to nest views inside of views? There are two ways to do it, and they are both easy. First, you can bind the view to a variable:
'GET /home' => function()
{
$view = View::make('user/login');
$view->content = View::make('partials/content');
$view->footer = View::make('partials/footer');
return $view;
}
Or, you can get the string content of a view using the get method:
<html>
<?php echo View::make('content')->get(); ?>
<?php echo View::make('footer')->get(); ?>
</html>
You will often need to redirect the browser to another location within your application. The Redirect class makes this a piece of cake. Here's how to do it:
'GET /redirect' => function()
{
return Redirect::to('user/profile');
}
Of course, you can also redirect to the root of your application:
return Redirect::to('/');
Need to redirect to a URI that should be accessed over HTTPS? Check out the to_secure method:
return Redirect::to_secure('user/profile');
You can even redirect the browser to a location outside of your application:
return Redirect::to('http://www.google.com/');
So far we've created redirects by specifying a URI to redirect to. But, what if the URI to the user's profile changes? It would be better to create a redirect based on the route name. Let's learn how to do it. Assuming the user profile route is named profile, you can redirect to the route using a dynamic, static method call like this:
return Redirect::to_profile();
Need to redirect to a route that should be accessed over HTTPS? No problem:
return Redirect::to_secure_profile();
That's great! But, what if the route URI looks like this:
'GET /user/profile/(:any)/(:any)' => array('name' => 'profile', 'do' => function()
{
//
})
You need to redirect to the named route, but you also need to replace the (:any) place-holders in the URI with actual values. It sounds complicated, but Laravel makes it a breeze. Just pass the parameters to the method in an array:
return Redirect::to_profile(array('taylor', 'otwell'));
The statement above will redirect the user to the following URL:
http://example.com/index.php/user/profile/taylor/otwell
After a user creates an account or signs into your application, it is common to display a welcome or status message. But, how can you set the status message so it is available for the next request? The Response and Redirect classes make it simple using the with method. This method will add a value to the Session flash data, which will be available for the next request:
return Redirect::to('user/profile')->with('status', 'Welcome Back!');
Note: For more information regarding Sessions and flash data, check out the Session documentation.
Perhaps you just want to force the web browser to download a file? Check out the download method on the File class:
'GET /file' => function()
{
return File::download('path/to/your/file.jpg');
}
In the example above, the image will be downloaded as "file.jpg", however, you can easily specify a different filename for the download in the second parameter to the method:
'GET /file' => function()
{
return File::download('path/to/your/file.jpg', 'photo.jpg');
}
While developing your application, you will probably need to generate a large number of URLs. Hard-coding your URLs can cause headaches if you switch domains or application locations. Nobody wants to dig through every view in their application to change URLs. Thankfully, the URL class provides some simple methods that allow you to easily generate URLs for your application.
To generate a URL for your application, use the to method on the URL class:
echo URL::to();
The statement above will simply return the URL specified in your application/config/application.php file.
However, this method can also append a specified string to the end of your application URL:
echo URL::to('user/profile');
Now the statement will generate a URL something like this:
http://example.com/index.php/user/login
Need to generate a HTTPS URL? No problem. Check out the to_secure method:
echo URL::to_secure('user/profile');
Often, you will need to generate URLs to assets such as images, scripts, or styles. You don't want "index.php" inserted into these URLs. So, use the to_asset method:
echo URL::to_asset('img/picture.jpg');
Alright, you have learned how to generate URLs from URIs, but what about from named routes? You can do it by making a dynamic, static method call to the URL class. The syntax is beautiful:
echo URL::to_profile();
Have a route that needs to be accessed over HTTPS? No problem:
echo URL::to_secure_profile();
Could it get any easier? Now, imagine a route that is defined like this:
'GET /user/profile/(:any)/(:any)' => array('name' => 'profile', 'do' => function()
{
//
})
To generate a URL for the route, you need to replace the (:any) place-holders with actual values. It's easy. Just pass the parameters to the method in an array:
echo URL::to_profile(array('taylor', 'otwell'));
The method above will generate the following URL:
http://example.com/index.php/user/profile/taylor/otwell
If you don't want to use dynamic methods, you can simply call the to_route method:
echo URL::to_route('profile', array('taylor', 'otwell'));
When writing an application like a blog, it is often necessary to generate URL "slugs". Slugs are URL friendly strings of text that represent something like a blog post title. Generating slugs is a piece of cake using the slug method:
echo URL::slug('My blog post title!');
The statement above will return the following string:
my-blog-post-title
Want to use a different separator character? Just mention it to the method:
echo URL::slug('My blog post title!', '_');
Need some convenient methods that make writing HTML a little less painful? Introducing the HTML class. Enjoy.
When displaying user input in your Views, it is important to convert all characters which have signifance in HTML to their "entity" representation.
For example, the < symbol should be converted to its entity representation. Converting HTML characters to their entity representation helps protect your application from cross-site scripting. Thankfully, using the entities method on the HTML class, it's easy to add this layer of protection to your Views:
echo HTML::entities('<script>alert('hi');</script>');
What trendy web application doesn't use JavaScript? Generating a reference to a JavaScript file is as simple using the script method.
echo HTML::script('js/scrollTo.js');
Referencing cascading style sheets is just as simple. Check out the style method:
echo HTML::style('css/common.css');
Need to specify a media type on your style sheet? No problem. Just pass it in the second parameter to the method:
echo HTML::style('css/common.css', 'print');
Note: All scripts and stylesheets should live in the public directory of your application.
Generating links couldn't be easier. Just mention the URL and title to the link method:
echo HTML::link('user/profile', 'User Profile');
Need to generate a link to a URL that should be accessed over HTTPS? Use the secure_link method:
echo HTML::secure_link('user/profile', 'User Profile');
Of course, you may generate links to locations outside of your application:
echo HTML::link('http://www.google.com', 'Search the Intrawebs!');
Any other attributes that should be applied to the link may be passed in the third parameter to the method:
echo HTML::link('http://www.google.com', 'Search the Intrawebs!', array('id' => 'google'));
If you are using named routes, you use intuitive, expressive syntax to create links to those routes via dynamic methods:
echo HTML::link_to_login('Login');
Or, if you need an HTTPS link:
echo HTML::link_to_secure_login('Login');
Now let's assume the login route is defined like this:
'GET /login/(:any)' => array('name' => 'login', 'do' => function() {})
To generate a link to the route, you need to replace the (:any) place-holder with an actual value. It's easy. Just pass the parameters to the method in an array:
echo HTML::link_to_login(array('user'));
This statement will generate a link to the following URL:
http://example.com/login/user
It's great to allow your users to get in touch with you, but you don't want to be bombarded by spam. Laravel doesn't want you to be either. The mailto method allows you to create a safe mail-to link that obfuscates your e-mail address:
echo HTML::mailto('example@gmail.com');
Want the link text to be something besides your e-mail address? No problem. Just mention it to the method:
echo HTML::mailto('example@gmail.com', 'E-Mail Me!');
Need to obfuscate an e-mail address but don't want to generate an entire mail-to link? Simply pass the e-mail address to the email method:
echo HTML::email('example@gmail.com');
Since you're building a creative application, you will need to include some images. The image method makes simple. Just pass the URL and Alt text to the method:
echo HTML::image('img/smile.jpg', 'A Smiling Face');
Like the link method, any other attributes that should be applied to the image may be passed in the third parameter to the method:
echo HTML::image('img/smile.jpg', 'A Smiling Face', array('id' => 'smile'));
Generating HTML lists doesn't have to be a headache. Check out the ol method. All it needs is an array of items:
echo HTML::ol(array('Get Peanut Butter', 'Get Chocolate', 'Feast'));
As usual, you may specify any other attributes that should be applied to the list:
echo HTML::ol(array('Get Peanut Butter', 'Get Chocolate', 'Feast'), array('class' => 'awesome'));
Need an un-ordered list? No problem. Just use the ul method:
echo HTML::ul(array('Ubuntu', 'Snow Leopard', 'Windows'));