PHP Frameworks: Laravel

Laravel’s creator describes it as a framework for people who value “elegance, simplic­ity, and readability.” It has well-thought-out documentation, a vibrant ecosystem of users, and available hosting providers and tutorials.

To install Laravel, run the command php composer.phar global require laravel/installer=~1.1″. Then, to create a new web project that uses Laravel, run laravel new project-name,1 substituting your project name for project-name. For example, running laravel new menu creates a directory called menu and populates it with the necessary code and configuration scaffolding for Laravel to work properly.

To see the scaffolding in action, fire up the built-in PHP web server by pointing it at server.php in your project directory. For example, php -S localhost:8000 -t menu2/public menu/server.php lets you access the new Laravel project in the menu subdirectory at http://localhost:8000.

Laravel’s routing is controlled by the code in app/Http/routes.php. Each call to a static method in the Route class tells Laravel what to do when an HTTP request comes in with a certain method and URL path. The code in Example 18-1 tells Laravel to respond to a GET request for /show.

Example 18-1. Adding a Laravel route

Route::get(‘/show’, function() {

$now = new DateTime();

$items = [ “Fried Potatoes”, “Boiled Potatoes”, “Baked Potatoes” ]; 

return view(‘show-menu’, [ ‘when’ => $now,

‘what’ => $items ]);

}); 

In Example 18-1, the call to Route::get() tells Laravel that it should be responding to HTTP GET (not POST) requests, and the first argument of /show tells Laravel that this Route::get() call provides information on what to do when the URL /show is visited. The second argument to Route::get() is the function that Laravel runs to compute the response to GET /show. This function sets up two variables, $now and $items, and then passes them to the show-menu view as keys when and what.

A view is a template that contains presentation logic—what your application should display. The view() function in Laravel looks for a file in a predefined location and then runs the PHP code in that file to generate a response. The call to view(‘show-menu’) tells Laravel to look for a file named show-menu.php in the resources/views directory. Example 18-2 contains the code for this view.

Example 18-2. A Laravel view

<p> At <?php echo $when->format(‘g:i a’) ?>, here is what’s available: </p>

<ul>

<?php foreach ($what as $item) { ?>

<li><?php echo $item ?></li>

<?php } ?>

</ul>

The view is just plain PHP. Any data coming from an external source such as a user or database should be properly escaped to prevent cross-site scripting problems, as discussed in “HTML and JavaScript” on page 138. Laravel includes support for the Blade templating engine, which makes many things easier, including escaping output by default.

Source: Sklar David (2016), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.

Leave a Reply

Your email address will not be published. Required fields are marked *