G
G
g9052019-02-03 03:14:33
Laravel
g905, 2019-02-03 03:14:33

How to set "/" route in Laravel module?

Hello.
There are module routes in modules.php:

<?php
Route::group(['namespace' => 'Gepard\Modules\Core\Controllers', 'middleware' => 'admin'], base_path('app/Modules/Core/Routes/routes.php'));
Route::group(['prefix' => '/admin', 'namespace' => 'Gepard\Modules\Admin\Controllers', 'middleware' => 'admin'], base_path('app/Modules/Admin/Routes/routes.php'));
Route::group(['namespace' => 'Gepard\Modules\Page\Controllers'], base_path('app/Modules/Page/Routes/routes.php'));
Route::get('/', 'Gepard\Modules\Page\Controllers\[email protected]');

Prefixes, hrenefixes, namespaces, base path to files with module routes are specified.
Obviously, "if we see the "admin" prefix, then we go to the /base_path/ file and take routes from there, inserting the module's namespace".
Ok, let's take the "Admin" module, here is its file with routes:
<?php
    Route::get('/', '[email protected]')->name('sdf');
    Route::get('/test/{id?}', '[email protected]');
    Route::get('/profile/{user?}', '[email protected]')->name('a.user');

I expect that the route "domain.local/admin" will show me index() from AdminController (Modules\Admin\Controllers\AdminController), and the route "domain.local/" will show me this
Route::get('/', 'Gepard\Modules\Page\Controllers\[email protected]');

So, on the route "domain.local/admin" they still show me everything I need, but on the route "domain.local/" they write to me that the AdminController controller was not found, damn it! Why? How did they get the idea that the "/" route needs a route from the group with the "admin" prefix?
I'm trying to remove the "/" from the "admin" group altogether. They write that CoreController was not found. This is because the "/" route is also defined there. Ok, let's remove it. And then they give me this one of yours
Route::get('/', 'Gepard\Modules\Page\Controllers\[email protected]');

That is, I can't create a route with just a dash "/". One prefix is ​​not enough. Why?
How do I create an index route for the module then? I want Gepard\Modules\Admin\Controllers\[email protected] to run at domain.local/admin and something else to run at domain.local/, let's say Gepard\Modules\Admin\Controllers\[email protected], well, or at least App\Http\Controllers\[email protected] What should I fix?
What is wrong with these namespaces?(
modulesServiceProvider
<?php namespace Gepard\Modules;
use Illuminate\Support\ServiceProvider;
/**
* Сервис-провайдер для подключения модулей
*/

class ModulesServiceProvider extends ServiceProvider {
    public function boot()
    {
        $modules = config('module.modules');
        if($modules)
        {
            foreach ($modules as $module)
            {
                if (file_exists(__DIR__.'/'.$module.'/Routes/routes.php'))
                {
                    $this->loadRoutesFrom(__DIR__.'/'.$module.'/Routes/routes.php');
                }
                //Загружаем View
                //view('Test::admin')
                if(is_dir(__DIR__.'/'.$module.'/Views'))
                {
                    $this->loadViewsFrom(__DIR__.'/'.$module.'/Views', $module);
                }
                //Миграции
                if(is_dir(__DIR__.'/'.$module.'/Migrations'))
                {
                    $this->loadMigrationsFrom(__DIR__.'/'.$module.'/Migrations');
                }
                //Переводы
                if(is_dir(__DIR__.'/'.$module.'/Lang'))
                {
                    $this->loadTranslationsFrom(__DIR__.'/'.$module.'/Lang', $module);
                }
            }
        }
    }

    public function register()
    {

    }
}

RouteServiceProvider
protected function mapModulesRoutes()
    {
        Route::middleware('web')
             ->group(base_path('routes/modules.php'));
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2019-02-03
@g905

Well, in my opinion, everything is obvious - in your ModuleLoader you load routes directly from __DIR__.'/'.$module.'/Routes/routes.php' (app_path() is better, but oh well), and, most likely, you connect this provider after the default Laravel one, which loads routes/modules
Either use one (and groups inside the corresponding modules is a more correct solution) or another (your modules.php, but this is a crazy implementation, given that laravel allows you to do more (and you have already done it))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question