Tuesday 21 February 2017

Topic (Laravel) 2 - Routing and Configuring Database

Basic Routing 


Previous lesson we have learn installing Laravel Framework via composer in Windows OS. Basically the first thing when you open a localhost website laravel is that you will notice the "route" thing on website URL.Such that  http://localhost/laraveltest/public/  will direct us to welcome page stated in our route.php. Eloquent 5.x changes route.php inside routes directory located at routes\web.php. 

Example of source code that direct welcome page by returning view model named welcome.php.
Route::get('/', function () {
    return view('welcome');
});

Route Parameters

Eventually Laravel Framework provide a future that enables view of specific segment divide according to each object. Such as to distinct or finding exclusive customer we can pass the parameter using 'customer/{id}' .

Route::get('customer/{id}', function ($id) {
    return 'Customer'.$id;
});

Database Configuration


The database of your laravel project is located at config/database.php. By default laravel will use mysql as default database connection and connect to localhost server. 
'default' => env('DB_CONNECTION', 'mysql'),

However you can change specific connection on this file according to your remote host server.

'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

    ],
Diiference between env file & config/database.php

That file (config/database.php) is the actual configuration file that will be used. That file just happens to pull some values from the env. This allows you to not have to change the config files themselves when you have a project on different servers. You can just have a different .env file on each host that specifies its unique values for those.
Imagine you are working with other people. They will have different configuration for their database. Having to hardcode values into the config file that is committed to a repository would end up with some problems as they pull down changes their configs might get overwritten. The .env file is not committed and should not be as its unique to the 'host'.
By having the config files pull values from env, each person (host) can have their own .env file with their own values without having to make changes to the actual config files.


References

https://laravel.com/docs/5.4/routing
https://laravel.io/forum/05-28-2016-env-file-vs-configdatabasephp

No comments:

Post a Comment