Laravel 5 – PHP Framework for Web Artisan
Let’s be honest Laravel is the best thing happened to PHP developers since the release of PHP. Recently Taylor Otwell the creator of Laravel has released version 5 of Laravel and it has not disappointed its followers as it comes packed with developer-friendly features. We will cover some of the major changes and the advantages of using Laravel. So without further ado let’s dig right in.
Laravel 5 introduces namespaces. By default, every app is namespaced as app but you can easily change it using simple command
php artisan app:name the-name The advantage of using namespace is that it avoids naming conflict between your app and third party libraries. It also helps you organise your code better.
Laravel 5 offers user authentication out of the box. Your fresh install will be able to register users and let them login using their email address and password. Not only form based authentication Laravel 5 also offers a package called Socialite which enables your users to register on your website using social websites like Facebook, Twitter, Google, GitHub and BitBucket.
Form Request object is another nifty feature which makes validating form data a cinch. You can create form request class by using a simple command like
php artisan make:request name-of-request Request class can be found in
app/Http/Requests folder and contains two methods ‘authorize’ and ‘rules’. In rules method, you define which form fields are required and then type hint the class in controller method where you will be handling form data. If a user submits the form without filling in required fields it will throw an error and redirect the user back to the form page.
Routes cache command will cache your routes file and will increase the speed of your website. Once you run
php artisan route:cache It will serialize routes file and future calls to routes will be served by cached file instead of routes which will significantly increase the speed of our app. You will need to clear the cache after making the changes to the app for them to appear on the website. You can clear the cache by using
php artisan route:clear command.
Middleware is another smart feature. Every HTTP request on your app goes through middleware so this is where you can perform business logic like making sure is user authorised to access the content. You can create middleware by using command
php artisan make:middleware the-name Your middleware class can be located in
app/Http/Middleware and contains a method ‘handle’, this is where you will write your business logic.
I don’t like waiting in queues in real life, but I simply love this feature in Laravel. Let me give you an example of how queues can help you. Let’s say user is registering on your website, User has filled in the form and pressed submit button now you need to perform few tasks before you can confirm to the user that they have successfully been registered. Tasks can be creating the user record in the database, sending the user an email and signing up the user for a newsletter. Now user don’t need to wait for all those tasks to finish so what we can do is create the user in the database and put other two tasks in the queue and send the response to the user. Laravel Queue will perform those tasks in the background while the user can carry on browsing the website. This makes our website more responsive and provides great user experience as no one likes to wait ages for a response from the website.
Commands and Scheduling
There are tasks that need to be automated and performed at specific time and date. Let me give you an example, Let’s say your boss has asked you for a daily report to be sent at 23:30. Now it won’t be convenient for yourself to come to work at midnight to generate the report. So what you can do is create a command in Laravel by typing
php artisan make:console command-name which will create a class and can be located at
app/Console/Commands You can define name and description in the class and it will have a method called ‘fire’ this is where you will create your daily report. Once you have coded the logic you can go to
app/Console/Command/kernel.php and in schedule method you can schedule your report by using syntax like
$schedule->command('name-of-command')->dailyAt('23:30') and you are done. Now your Laravel app will send your daily report to your boss at 23:30 every day.
Laravel 5 Elixer provides us with neat API which helps us define Gulp tasks. It can compile Sass, Less and coffeScript. It can combine CSS and JS files and output to your required folder. It also supports testing tools.
To sum up Laravel 5 provides us with enough features to build a complex app with ease. You can learn more about Laravel 5 and read its documentation here.