Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for creating administrator interface in Laravel 4

I would like to create an administrator interface for my Laravel project, which is completely separated from the user side.

For example, in Yii framework I can make a module and this will ensure full separation from the user side. Inside a module I can use separate folder structure etc.

like image 574
Nagy Ervin Avatar asked Apr 25 '14 14:04

Nagy Ervin


People also ask

How to Set up admin panel in Laravel?

To add admin panel in Laravel, just copy and paste all the CSS and JS files/folders into the vendor folder, and then call these files in the app. blade. php file.

Does Laravel have an admin panel?

Voyager is a free and open-source admin panel for the Laravel application. It will take care of your administrative tasks and database tables. Here are some features of Voyager: Voyager has BREAD(Browse, Read, Edit, Add and Delete) functionality.

What is Laravel admin?

laravel-admin is administrative interface builder for laravel which can help you build CRUD backends just with few lines of code. Since I don't have much time to maintain this project, laravel-admin only supports the LTS version of Laravel (currently Laravel 5.5). Demo use username/password:admin/admin.


1 Answers

This is really a broad question and one answer can't cover everything about best practice for admin controllers or back end management but there are some basic concepts for building an Admin Panel:

// Keep all of your admin routes inside something like this
Route::group(array('prefix'=> 'admin', 'before' => 'auth.admin'), function() {

    // Show Dashboard (url: http://yoursite.com/admin)
    Route::get('/', array('uses' => 'Admin\\DashBoardController@index', 'as' => 'admin.home'));

    // Resource Controller for user management, nested so it needs to be relative
    Route::resource('users', 'Admin\\UserController');

});

// Other routes (Non-Admin)
Route::get('login', array('uses' => 'AuthController@showLogin' 'as' => 'login'));

By using a prefix you may separate all admin routes whose url will be prefixed with admin so, if you have a users controller for user management in back end then it's url will be prefixed with admin, i.e. site.com/admin/users. Also using a before filter you may add an authentication for all admin controllers in one place, that means, to access all of your admin controllers user must be logged in and the filter could be something like this:

Route::filter('auth.admin', function($route, $request, $args){
    // Check if the user is logged in, if not redirect to login url
    if (Auth::guest()) return Redirect::guest('login');

    // Check user type admin/general etc
    if (Auth::user()->type != 'admin') return Redirect::to('/'); // home
});

For, CRUD (Create, Read, Update, Delete) use a resourceful controller, for example, the UserController in an example of resourceful route declaration.

Use repository classes (Repository Pattern) for decoupling of dependencies, read this article.

Always use a named route, i.e. array('as' => 'routename', 'uses' => 'SomeController@method'), this is an example of naming a route. Named routes are easy to refer, i.e. return Redirect::route('admin.home') will redirect to site.com/admin because we have used admin.home in as to assign the name for that route.

Keep admin controllers in a separate folder and use a namespace for example, Admin\\DashBoardController@index controller should be in app/controllers/admin and your DashBoardController controller should look like this:

<?php namespace Admin;

class DashBoardController extends \BaseController {
    public function index()
    {
        //...
    }
}

There are more but it's enough to start with, read articles online and must read the documentation.

like image 145
The Alpha Avatar answered Sep 23 '22 01:09

The Alpha