Getting started with Laravel 5

June 18, 2021

Tags: Technologies, IT Staff Augmentation

Laravel, is one of the framework in PHP that is having greater acceptance in recent years due to the simplicity of the syntax, elegance in writing, the power of [Composer](https://getcomposer.org /) and Artisan for handling and supplements are there, make PHP not be forgotten and then be rescued. Thanks to Laravel we can have our projects with a modern language, fast, efficient and professional.

 

Laravel has evolved over recent years, with the release of version 5 can notice how mature is this framework; to test the power create or activate the authentication module that provides Laravel with installation.

 

Let's start from the beginning (Installation).

 

Our faithful friend Composer is responsible for conducting much of the work for us when installing.

 

With the composer create-project command we can begin installation automatically, the will download all components of the framework.

 

create-project

The package is called framework laravel/laravel and directory where the project we have called Laravel5 be created. By completing our facility our files are as follows:

estructura-laravel

We must emphasize that in this version the structure was changed to better control the flow of the application, we focused mainly on the app folder, it can get the routes and controllers.

 

To test our installation we just execute the command console php artisan serve

artisan-serve

As we note in our console, our app is listening on port 8000, copy the address passed in the browser.

run-laravel

Perfect, pretty clean installation.

Now let a little detail like comment at the beginning of the publication we want to implement the authentication service that incorporates laravel, for it mainly begin by reviewing the routes which is in the routes.php file (to access it we we go to the app/Http/routes.php) folder.

 
Route::get('/', 'WelcomeController@index');

Route::get('home', 'HomeController@index');

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

 

If we pay attention to the routes declared we note that there are 2, the root (which is the index which just entered) and the path home. Let's try the latter.

login-view

Interestingly, when we tried to access the home route we redirected to a login screen default laravel, to implement the functionality of login need to perform several steps because if we tried logged it will generate an error indicating that the users table does not exist, start with these steps.

 

Mainly we create our database for the application and set in our environment variables.

create-database

With the database created must access the file .env which it is at the root of our directory, in the values we setear to connect successfully.

 

 
APP_ENV=local
APP_DEBUG=true
APP_KEY=pIVKIjshiWC0GCKiE0pFGGO45L2BDJIX

DB_HOST=localhost
DB_DATABASE=laravel5
DB_USERNAME=TuUsuario
DB_PASSWORD=TuPassword

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null

 

If we look at the configuration file (config/database.php) connection with database we note that makes calls on the variables in this configuration.

 

 
'connections' => [
        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],
    ],

 

Up to this point we have set our application, we are missing an important point is the creation of the table of users to access through authentication form, for this point will use the migrations or migrations. If we go to the database / migrations directory, look at two migration files created with the structure of the tables.

 

 
class CreateUsersTable extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

 

To perform these migrations we run the php artisan migrate command

 

migrate-database

If we review our database we note that have created two new tables and a reference table of the executed migration.

show-tables

At this point our application would be functional, let's try recording in the users table and testing the login provided by Laravel. Access the route http://localhost:8000/auth/register and generate a user.

register-user

If all went well we should have a window like this when registering the user.

login

We review the record in our table

select-user

and finally we test the auth method provided by Laravel.

session

We already have a login system with only some configurations, this gives us an idea of the scope that can have this framework that as I mentioned them at the beginning has improved considerably with this new version.

If you are starting a new project or want to learn new technologies, undoubtedly Laravel may be a great choice, your learning curve is much shorter than many of the frameworks in PHP. As its slogan The PHP Framework For Artisans Web.

Let's work together!