I
I
Ilya2018-05-27 21:18:57
Laravel
Ilya, 2018-05-27 21:18:57

What needs to be done in Laravel 5.6 so that the framework understands this construct?

Hello Everyone
Recently I found an interesting repository https://github.com/z-song/laravel-admin for the admin panel. I liked the fact that it does not "shit" its files into the application, that is, the admin panel is rigidly separated from the structure by the Admin daddy in the App \ Admin section. I decided to transfer all my admin controllers to the same folder on the test branch. But how to make laravelka see the controllers in this folder. The structure should be something like this:
App
-- Admin
------ Controllers
---------- DashboardController.php
------ Extensions
------ Middleware
------ e / t / c to taste ...
Registered in the file routes\web.php

use Illuminate\Routing\Router;

Route::group([
    
    'prefix'        => 'admin',
    'namespace'     => 'App\\Admin\\Controllers',
    'middleware'    => ['web'],

], function (Router $router) {
    
    $router->get('/welcome', function () {
    	return view('welcome');	
    });
    $router->get('/dashboard', '[email protected]');
    
});

In DashboardController
namespace App\Admin\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class DashboardController extends Controller
{
    public function index()
    {
    	return view('welcome')
    }
}

But stsabaka gives an error. 5b0af5fea668f790313732.jpeg
It always adds the construct at the beginning of App\Http\Controllers to any Namespace in such a route. Middweir container @[email protected] is a standard kit that comes with the installation of the system, there is nothing even logins.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2018-05-27
@flammerman

It seems to have found a solution, I don’t know if there will be pitfalls
In the App / Admin folder, put the routes.php file

use Illuminate\Routing\Router;

Route::group([
    
    'prefix'        => config('admin.route.prefix'),                 // 'admin'
    'namespace'     => 'App\\Admin\\Controllers',
    'middleware'    => config('admin.route.middleware'),  // ['web']

], function (Router $router) {
    
    $router->get('/welcome', function () {
    	return view('welcome');	
    });
    $router->get('/dashboard', '[email protected]');
    
});

Next, we make a service provider for the admin panel: (AdminServiceProvider.php)
namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AdminServiceProvider extends ServiceProvider
{

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot() // Собственно эта функция и есть мое решение (грузит роуты для админки)
    {
        if (file_exists($routes = config('admin.admin_dir').'/routes.php')) {
            $this->loadRoutesFrom($routes);
        }
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

We register the service provider in the app.php config, at the bottom there as usual, you will see who is a newbie such lines
and add one more line there:
Well, everything seems to be, we see any controllers along the routes example.com/admin/some_action

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question