In this post we’ll create a simple, base Laravel project template that we’ll expand upon in future write ups to explore items such as roles and permissions, validation, CRUD via React, and so forth.
You can find the complete source code for this post on GitHub.
Create the configure the base Laravel implementation
To start we create a new Laravel instance utilizing Composer:
$ composer create-project --prefer-dist laravel/laravel AppTemplate
$ cd ./AppTemplate/
$ php artisan key:generate
Next, connect the SQL server of your choice to the application, and create an app_template
database. Don’t forget to update the config/database.php
and .env
files with your database connection details.
Example:
// **********
// config/database.php
// **********
'default' => env('DB_CONNECTION', 'pgsql'),
'pgsql' => [
'driver' => 'pgsql',
'url' => env('127.0.0.1'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'app_template'),
'username' => env('DB_USERNAME', 'postgres'),
'password' => env('DB_PASSWORD', 'password'),
// **********
// .env
// **********
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=app_template
DB_USERNAME=postgres
DB_PASSWORD=password
Laravel’s out-of-the-box authentiation
We’ll also implement Laravel’s out-of-the-box authentication for future customization:
$ php artisan make:auth
$ php artisan migrate
This will make the following changes:
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: routes/web.php
Untracked files:
(use "git add <file>..." to include in what will be committed)
app/Http/Controllers/HomeController.php
resources/views/auth/
resources/views/home.blade.php
resources/views/layouts/
And we can view the new routes that were added–which we’ll customize later–with the following command:
$ php artisan route:list
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | GET|HEAD | home | home | App\Http\Controllers\HomeController@index | web,auth |
| | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController@showLoginForm | web,guest |
| | POST | login | | App\Http\Controllers\Auth\LoginController@login | web,guest |
| | POST | logout | logout | App\Http\Controllers\Auth\LoginController@logout | web |
| | POST | password/email | password.email | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail | web,guest |
| | GET|HEAD | password/reset | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest |
| | POST | password/reset | password.update | App\Http\Controllers\Auth\ResetPasswordController@reset | web,guest |
| | GET|HEAD | password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController@showResetForm | web,guest |
| | GET|HEAD | register | register | App\Http\Controllers\Auth\RegisterController@showRegistrationForm | web,guest |
| | POST | register | | App\Http\Controllers\Auth\RegisterController@register | web,guest |
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
React
We also want React support for use in our future development efforts:
$ php artisan preset react
$ npm install && npm run dev
Test what we’ve done so far
Let’s start the application and ensure everything is working as expected so far:
$ composer dump-autoload && php artisan cache:clear && php artisan serve
Once we browse to http://127.0.0.1:8000/
we should see this familiar screen if everything went correctly:
So far so good!
React testing
Let’s test the React functionality at this point now too. We just need to make a few changes:
- Edit the
resources/views/layouts/welcome.blade.php
file, and add the following after the list of Laravel<li>
links:
<div id='example' />
Then add this code just before the </body>
tag as shown below:
<!-- Laravel Application Core JS -->
<script src="{{ mix('js/app.js') }}"></script>
</body>
</html>
When the page is reloaded we should see the following, which indicates that React is working correctly:
Generate users
And finally we should generate some users to test the authentication as well as set us up for the next write up. We start by creating a User seeder:
$ php artisan make:seeder UserSeeder
Next, add the following code to the database/seeds/UserSeeder.php
file:
<?php
use Illuminate\Database\Seeder;
use App\User;
class UserSeeder extends Seeder
{
/**
* Create application users.
*
* @return void
*/
public function run()
{
// Create an admin user
$user = User::create([
'name' => 'Admin',
'email' => 'admin@admin.com',
'password' => bcrypt('password')
]);
$user->save();
// Create dev/test data for non-production environments
if (env('APP_ENV') != 'production') {
// Create N mumber of users
factory(User::class, 20)->make()->each(function($user) {
$user->save();
return true;
});
}
}
}
This will create not only an admin user, but twenty other random users as well.
Note that the factory(…) function in the code above is using the instructions found in the database/factories/UserFactory.php file to actually create the User objects, populate their attributes, and then save them into the database.
In order to have the seeder populate the database we add the seeder to the database/seeds/DatabaseSeeder.php
file:
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call(UserSeeder::class);
}
}
Now we can utilize the seeder to create a sample of new user accounts with the following command:
$ php artisan db:seed
Final testing
We are now ready to log in as the Admin user we created:
- Fill out the login form….
- And success!
Summary
We now have a working Laravel application base template that can be utilized to develop further functionality, and that is exactly what we’ll do in the next set of write ups. :)
You can also find the source code for this post on GitHub.
If you have any comments or questions please don’t hesitate to reach out.
Thanks!
Comments