Best practice for creating administrator interface in Laravel 4 Best practice for creating administrator interface in Laravel 4 php php

Best practice for creating administrator interface in Laravel 4


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 thisRoute::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.


If you are familiar with composer you can import in packages (aka modules)

There is a widely available module with multi level interface already called Sentry 2.0:https://github.com/cartalyst/sentry

You could also make your own if needed if the one I propose is too complex.

There is even a "laravel-ready" version of sentry.


I use the same directory structure that you would like to use on most (if not all) my Laravel projects. Basically, I keep admin views and admin controllers separate from the front-end ones.

Examples:Controllers:

app/controllers/admin/Admin*Name*Controller.phpapp/controllers/site/*Name*Controller.php

Views:app/views/admin/some_folder/index.blade.phpapp/views/site/some_folder/index.blade.php

I would also suggest that you install this laravel project https://github.com/andrewelkins/Laravel-4-Bootstrap-Starter-Site which will give a very good starting on how to organise things in your laravel project. It also has the same folder structure you would like to use.

Good luck.