Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for creating administrator area interface in Laravel 5

I have recently started learning Laravel 5 and i need to create a seperate administrator area within the site.

I have tried a lot to get the file directory structure but most of them i got are for Laravel 4 and not for the Laravel 5.

As Laravel 5 differs in the structure from Laravel 4.

Can anyone please help me how to setup the the directory structure for the administrator area and the routings.

Thank you in advance.

like image 878
Veerendra Avatar asked Jun 22 '15 04:06

Veerendra


2 Answers

I had recently create a project in Laravel 5 with Admin area. I had used advanced ACL (Access Control Layer) to give role wise admin permissions. For this I had used Entrust Package. Then I used the basic auth of Laravel 5 and had implement Entrust.

I had easily created the admin area with all the permissions. I have also tried to set automatic permissions within Authenticate.php middleware

You can reference this Entrust Automate stack question, I had asked. But later I found the solution. I would suggest you to follow URL based architecture then the old way of folder structure to manage the Admin. Laravel 5 is best for URL based which will also help you to create any API's if you need in future for mobile apps.

Hope this helps you.

like image 181
Tarunn Avatar answered Oct 21 '22 19:10

Tarunn


I usually make my own back-end, with packages like entrust and a random bootstrap template that fits my needs.
Then I just put my views in views/admin and my controllers in Controller/admin and put all admin routes in a group with a middleware attached to it

    Route::group(['namespace'=>'Admin','prefix'=>'admin','middleware'=>'role','role'=>'admin'],function(){
        Route::get('/','HomeController@index');
}

and then in the middleware (this is using entrust).

public function handle($request, Closure $next)
    {
        $user = $this->auth->user();
        $route = $request->route();
        if($user && $route)
        {
            $actions = $route->getAction();
            if(array_key_exists('role',$actions)) {
                $role=$actions['role'];
                if(!$user->hasRole($role)) {
                    Flash::error('Unauthorized Access');

                    abort(401);
                }
            }
            else
            {
                Flash::error('Unauthorized Access');
                abort(401);
            }
        }
        else
        {
            Flash::error('Unauthorized Access');
            abort(401);
        }
        return $next($request);
    }

Don't forget to register the middleware in the kernel.php next to the routes file.

The middleware might look complicated but is simply checks if you have put a role in your group en then if the currently logged in user belongs to that group.

Also remember that when you route using action(), you need to put the admin namespace before the controller name like action('Admin\HomeController@index')

like image 23
aranna Avatar answered Oct 21 '22 19:10

aranna