9

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.

Nagy Ervin
  • 701
  • 2
  • 11
  • 21

3 Answers3

24

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.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • I think like \Basecontroller, i will have to include all helper like Request, View etc whichever required in the controller method, otherwise, error will thrown like Request class not found something. How to come out of this issue? – Neeraj Dec 08 '14 at 15:13
  • Yes, if `namespace` is used then you need to use `\` or add `use` statement top of the class like `use Request;`. – The Alpha Dec 08 '14 at 16:02
  • I found another best way is to simply keep your all controllers in admin folder and autoload them into composer.json then in that case there will no need to use namespace or add helpers manually. – Neeraj Dec 08 '14 at 18:51
2

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.

azngunit81
  • 1,574
  • 2
  • 20
  • 38
  • unfortunately i am not familiar with composer, but i would like to learning/using it. Thank you – Nagy Ervin Apr 25 '14 at 14:36
  • well its your primary way to get Laravel. Checkout Jeffrey Way's excellent Laravel deployment tutorial https://laracasts.com/lessons/laravel-installation-for-newbs - it shows you composer - as from now on you should start being familiar with it if you want to advance in better programming and to not reinvent the wheel. – azngunit81 Apr 25 '14 at 14:40
0

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.php app/controllers/site/*Name*Controller.php

Views: app/views/admin/some_folder/index.blade.php app/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.

Keith Mifsud
  • 1,599
  • 1
  • 16
  • 26