t0berius's avatar

exclude admin panel as folder

Because I would like to have my admin panel be seperated from the main application I would like to seperate the whole admin panel to a folder(admin). This makes it easier for me to set for example some rules on my server, so I can only access the admin direction when I'm connected to my server and use a tunneled connection.

What's the best way to exclude all controllers/models/views to another folder so I exclude all main content of admin panel from main app.

0 likes
6 replies
bobbybouwmann's avatar

You can give your admin classes their own namespace, for example App\Admin\Http\Controllers\PostsController. Or you can do it like so App\Http\Controllers\Admin\PostsController. That is up to you. The last one is easier, since you don't have to adjust anything for reading your routes.

t0berius's avatar

@bobbybouwmann I tried to change just the namespace of my new controller (AdminController). What to change now in routes? Doesn't work, how did you mean the last option? Don't understand what to change.

bobbybouwmann's avatar

Well if you have a controller like so

\\ app/Http/Controllers/Admin/PostsController.php

<?php

namespace App\Http\Controllers\Admin;

class PostsController extends Controller { // Or even extend your own base AdminController
    
    public function index() {}  

}

You can then define the route like so

Route::group([ 'namespace' => 'Admin', 'prefix' => 'admin'], function () {
    Route::get('posts', ['as' => 'posts.index', 'uses'=>'PostsController@index']);
});
t0berius's avatar

I tried to modify it to my need.

route:

Route::group([ 'namespace' => 'Admin', 'prefix' => 'admin'], function () {
    Route::get('admin', ['as' => 'admin.index', 'uses'=>'AdminController@index']);
});

controller (path: Http\Controllers\Admin\AdminController.php):

 <?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class AdminController extends Controller
{
    public function index()
    {
        dd("ya");
    }
}

returns a 404 page.

martinbean's avatar
Level 80

@jaheller You don’t need to include admin in the route pattern if you’ve added a prefix of admin to the group, otherwise the route will be /admin/admin. Run php artisan route:list and you will see what I mean.

In your case, you’ll need to do this:

Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function () {
    Route::get('/', 'AdminController@index')->name('admin.index'); // Will match /admin/
});

You’ll probably also want to stick some middleware on that group to ensure only authenticated administrators can view admin routes.

martinbean's avatar

@jaheller There’s some middleware that handles maintenance mode: https://github.com/laravel/framework/blob/5.2/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php

It intercepts all requests and if maintenance mode is enabled, returns a 503 (service unavailable) HTTP exception. You’ll need to create your own version and replace Laravel’s if you want to still allow requests to your admin routes. Something like this:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Foundation\Http\CheckForMaintenanceMode as LaravelCheckForMaintenanceMode;
use Symfony\Component\HttpKernel\Exception\HttpException;

class CheckForMaintenanceMode extends LaravelCheckForMaintenanceMode
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     *
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
     */
    public function handle($request, Closure $next)
    {
        // Check request is not for admin panel
        if (! $request->is('admin/*')) {
            if ($this->app->isDownForMaintenance()) {
                throw new HttpException(503);
            }
        }

        return $next($request);
    }
}

Then replace the line in your app/Http/Kernel.php file to use your middleware instead of Laravel’s:

protected $middleware = [
    \App\Http\Middleware\CheckForMaintenanceMode::class,
];

The above middleware class will check if the URI begins with “admin” and if so, let’s requests through. If it doesn’t, it’ll then check if maintenance mode is enabled and show the appropriate response.

Also, you’ll need to update the namespace if you’re not using the App default.

Please or to participate in this conversation.