Why you should use the Laravel web framework

Why you should use Laravel

Why you should use Laravel

Are you interested in building a web application, or new platform, and still choosing which frameworks to use? Are you familiar and like working with PHP? If so, you may want to learn about Laravel, and why it is a great web framework.

Laravel is a framework for creating web applications with an easy to use syntax. The idea behind Laravel is that it makes common development tasks easy such as routing, authentication, sessions, and caching.

Let’s dive into some of the main reasons that many people are choosing to use the Laravel web framework.

Reasons to use Laravel

  • It has a large growing community
  • Laravel has a great ORM and database layer (eloquent).
  • Routing is easy and simple to do.
  • The functional core can be extended.
  • It can easily be integrated with other third-party libraries. To do this, you can use Composer or Packagist.
  • Unit testing is supported out of the box.

As you can see, there are many reasons to use Laravel, and we will be diving deeper into them later in this article. First, why is Laravel popular?

Popularity

Laravel was developed by Taylor Otwell and was released in June of 2011. Since then, the community has grown to help those users who may be facing any issues with using Laravel. Due to popular frameworks and CMS platforms, PHP has also grown in popularity for developers. This makes for a strong network to lean on for guidance, and has become one of the main reasons why Laravel has grown in popularity.

The Eloquent ORM

Laravel uses the Eloquent ORM. You can learn more by reading the official documentation here: https://laravel.com/docs/5.8/eloquent

To quickly explain, there is a Query Scope, which means query logic is generated as a function with a scope prefix.

Before using the ORM, you need to have your database setup. Each table in the database has a corresponding Model which is used to interact with that table.

Below is an example of a standard SQL Query translated to a query function for the Eloquent ORM:

SELECT * WHERE hub_id = 100 AND (name LIKE `˜%searchkey%` OR surname LIKE `˜%searchkey%`): 
function scopeByFieldListLike($query, $fields, $value){
    $query->where(function($query) use ($fields, $value){
        foreach($fields as $field){
            $query->orWhere($field , 'like', "%".$value."%");
        }
    });
    return $query;
}

Below is an example of how to create a function:

$model = new User;
$model->byFieldListLike(['name', 'surname'], 'searchkey');

There you have it. You can briefly see what it is like to work with Eloquent ORM.

The Eloquent ORM is a beautiful, and simple ActiveRecord implementation for working with your database.

Routing

What is a route anyways? A route is a way of generating a request URL of your application. The URL does not have to connect to specific files on a website. The best thing about these URL’s is that they are both readable by humans, and the crawlers to make the site SEO friendly.

Routes are created inside the routes folder. This applies to Laravel 5.5. Routes for the website are created in the web.php file, Similarly, the routes for the API are created inside the api.php file.

View Larevel kernel on GitHub. The Kernel implements an IoC pattern which allows developers to customize and rewrite the framework such as authentication, requests, logging, etc. This cuts the time down in creative development, you do not need to reinvent the wheel.

Like Ruby on rails routing is simple, group the routs, and generate resources for CRUD pages, you can also attach filters and automatically bind models to the request parameters.

Let’s learn more about routing for Laravel using example code. Below is an example of the nested routes:

Route::group(['prefix'=>'level0'], function(){
   	Route::get('/', array('uses' => 'TestController@level0'));
    Route::group(['prefix'=>'/{level0}/level1'], function(){
   	    Route::get('/', array('uses' => 'TestController@level1'));
	    Route::post('/{custom_variable}/custom_route', array('uses' => 'CustomConroller@custom_route'));
    });
});

You can use the ‘v1’ prefix for nesting the versioning as a group. When modifying the API, you can use the old one with the ‘v1’ prefix, and make such implementations on ‘v2’ prefix for routes with different code or logic.

As the example code above, you can define a group of routes with the path level0 at the top level of the API.

If you have a call BASEURL/level0 Laravel will the resolve it. It will then call the method level0() of TestController to process the request.

There is a sub-group: {level0}/level1 pattern. If you would like to access API resources inside this group then you should use the following path for the parent group (level0) then match the pattern of the sub-group ({level0}/level1).

Unit Testing

Although writing unit tests can be time-consuming, however, it’s safe and worthwhile to do so. Larvel is great for testing and comes out of the box with the capabilities to start testing. For example, you can use the phpunit.xml for PHPUnit testing. In addition to the phpunit.xml file, Laravel also comes with ExampleTest.php which is meant for running tests on the Feature and Unit directories. Such tests can be run by using the phpunit command.

Laravel uses its TestCase base class when unit testing. It creates a new instance of the application, resolves the route and runs the controller method in its own testing environment.

Unfortunately, it does not run application filters such as (App::before and App::after) or more complex routing scenarios. To enable these filters in its own testing environment, you will have to manually enable them.

You can find more information about testing by visiting the following page: https://laravel.com/docs/5.8/testing

Getting started

Setting up Laravel, and building your application will be unique for your specific project. You can, however, take a look at this checklist to get started:

  • Create and setup a homestead Vagrant box for development using Laravel and configure your local environment.
  • Add endpoints (configure routes).
  • Add authentication layer (good for privacy).
  • Add filters before/after application execution, giving you an option to run anything before and after application is executed. Additionally, you can define filters for any route.

Below is an example of the “before” filter.

	    class AppBefore {
			function filter($request) {
	    	    \App::singleton('scope', function() {
            		$scope = new AppScope();//My scope implementation
        	    	$scope->startTimer();
	    	        return $scope;
		        });
    		}
		}

Here is an example of the “after” filter.

		class AppAfter{
		    function filter($request, $response){
        		$response->header('Access-Control-Allow-Origin', '*');
		        $response->header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
        		$response->header('Access-Control-Allow-Headers', 'Content-Type');
		        $response->header('Access-Control-Max-Age', '86400');
        		\App::make('scope')->endTimer();
		        \App::make('scope')->logTotalTime();
        		return $response;
		    }
		}

There you have it. Now you know a bit about Laravel, and how to get started, you may want to use it for your next project. You can learn more by reading the following video:

Leave a comment

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