Laravel 5 Admin Panel
Introduction
Users do not want to be constantly calling you whenever they want to update data. They want to do it themselves. For heaven’s sake you also need some “you time” and not always doing updates for the users.These tutorial series will guide you through the process of creating an administrative panel for your Laravel powered web applications. The admin panel will be used by the users to update data and perform other tasks.
For the sake of completeness, we will create the admin panel for Larashop. Larashop is a tutorial project that we created in the Laravel 5 tutorial series. For the sake of learning, we will create a new project and not built on top of Larashop. If you want to, you can use Larashop project. We will work with a modern open source admin panel. The admin panel uses HTML5, twitter bootstrap and CSS3. You can download the HTML template from the GitHub repository.
The following image shows how our customized template will look like.
Topics to be covered
We will cover the following topics in tutorial;- Tutorial prerequisites
- Topics to be covered in the tutorial series
- Converting HTML 5 Template to Laravel Blade Templates
- Admin Panel Controllers
- Admin Panel Routes
Tutorial prerequisites
This tutorial assumes;- You understand the basics of Laravel 5. If you do then I recommend you start with these Laravel 5 tutorial series.
- You have PHP, MySQL and Apache up and running
- You have composer installed
- You have a text editor or IDE that supports PHP.
- You have a modern web browser that supports HTML5
Topics to be covered in the tutorial series
We will cover the following topics in these tutorial series.- Laravel 5 Eloquent Relationships
- Laravel 5 Users and Roles Management
- Laravel 5 Social Authentication
- Laravel 5 Localization
- Laravel 5 Storage
- Laravel 5 Mail
- Laravel 5 CSV
- Laravel 5 PDF
- …and many more topics
Converting HTML 5 Template to Laravel Blade Templates
Let’s start with downloading the Admin template from its GitHub RepositoryUnzip the contents of the download file. Have fun exploring the admin template.
We will now create a new Laravel 5 project.
I am using windows and XAMPP. I will now browse to the web directory of XAMPP
cd C:\xampp\htdocs
composer create-project laravel/laravel laradmin 5.*
- The above code creates a Laravel 5 project.
*
fetches the latest minor version number.
5.2
you can use the following command to specify version 5.2
when creating the new project.composer create-project laravel/laravel laradmin 5.2
Let’s now look at the above directories
- Admin – this is the directory that will contain all of our template files
- E_shop_data_entry – contains the template files for all items under the E-Shop Data Entry panel in the admin panel
brands.blade.php
– lists all brands and modal form for creating and updating brandscategories.blade.php
– lists all categories and modal form for creating and updating categoriesproducts.blade.php
– lists all products and modal form for creating and updating products- E_shop_transactions – contains the template files for all items under the E-Shop Transactions panel in the admin panel
customers.blade.php
Lists all registered customersorders.blade.php
Lists all customer ordersproducts.blade.php
Lists all ordered products- Frontend_data_entry contains the template files for all items under the Frontend Data Entry panel in the admin panel
blog_posts.blade.php
lists all the blog posts and modal form for creating and updating blog postspages.blade.php
lists all the pages and modal form for creating and updating pages- Layouts – contains all master layout files.
layout.blade.php
– contains code that is common to all pages i.e. headers, navigation, footer etc.- System – contains all templates files for items under the System Panel in the admin panel
database_backup.blade.php
– displays the form for making database backupsexport_to_csv.blade.php
– provides options for exporting database records to the comma separated files (CSV)import_from_csv.blade.php
– provides options importing CSV file records into the database.system_users.blade.php
– provides options for managing users in the system.
layout.blade.php
template will have the following common format<html>
<head>
<title>@yield('title')</title>
</head>
<body>
@section('sidebar')
This is the master sidebar.
@show
<div class="container">
@yield('content')
</div>
</body>
</html>
@extends('layouts.master')
@section('title', 'Page Title')
@section('content')
<p>This is my body content.</p>
@endsection
Admin Panel Controllers
Just like we did with the templates, we will also adopt a modular approach with our controllers for better file organization. We will create the following controllers;- EShopDataEntry
- BrandsController
- CategoriesController
- ProductsController
- EShopTransactions
- CustomersController
- OrdersController
- ProductsController
- FrontendDataEntry
- BlogPostsController
- PagesController
- System
- UsersController
- SystemController
php artisan make:controller EShopDataEntry\BrandsController --resource
php artisan make:controller EShopDataEntry\CategoriesController --resource
php artisan make:controller EShopDataEntry\ProductsController --resource
php artisan make:controller EShopTransactions\CustomersController --resource
php artisan make:controller EShopTransactions\OrdersController --resource
php artisan make:controller EShopTransactions\ProductsController --resource
php artisan make:controller FrontendDataEntry\BlogPostsController --resource
php artisan make:controller FrontendDataEntry\PagesController --resource
php artisan make:controller System\UsersController --resource
php artisan make:controller System\SystemController
Let’s now create the routes
Admin Panel Routes
Open/app/Http/routes.php
Update the code to the following
<?php
/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('welcome');
});
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::group(['middleware' => ['web'], 'prefix' => 'admin'], function () {
//Dashboard Route
Route::get('dashboard', function() {
return view('admin.dashboard');
});
//EShop Data Entry Routes
Route::resource('brands', 'EShopDataEntry\BrandsController');
Route::resource('categories', 'EShopDataEntry\categoriesController');
Route::resource('products', 'EShopDataEntry\ProductsController');
//EShop Transactions Routes
Route::resource('customers', 'EShopTransactions\CustomersController');
Route::resource('orders', 'EShopTransactions\OrdersController');
Route::resource('product-sales', 'EShopTransactions\ProductSalesController');
//Frontend Data Entry Routes
Route::resource('blog-posts', 'FrontendDataEntry\BlogPostsController');
Route::resource('pages', 'FrontendDataEntry\PagesController');
//System
Route::resource('system-users', 'System\UsersController');
Route::resource('database-backup', 'System\SystemController@dbbackup');
Route::resource('csv-export', 'System\SystemController@csvexport');
Route::resource('csv-import', 'System\SystemController@csvimport');
});
Route::group(['middleware' => ['web'], 'prefix' => 'admin'], function () {…}
groups the routes, applies the webmiddleware
and sets theadmin
prefix. The prefix will be automatically appended to the beginning of every route defined inside the group.Route::resource(…)
the resource route maps the HTTP verbs to the appropriate methods in the respective controllers.
Testing the waters
Laravel has a built in web server which you can invoke from the command line.Open the terminal / command prompt
Browse to the project root. You can use the following command if you are using windows and xampp
cd C:\xampp\htdocs\laradmin
php artisan serve
8000
Load the following URL in your web browser
http://localhost:8000/admin/brands