php laravel 5中多个路由文件而不是一个主路由文件

bqucvtff  于 2022-12-10  发布在  PHP
关注(0)|答案(9)|浏览(158)

我是一个新手在网页开发与Laravel 5。我安装了asGgardCMS和后,看到asgardCms代码,我发现app/Http/route.php文件中没有代码,而路由所需的代码应放在Modules代码中。例如,路由菜单管理器模块所需的代码应放在Modules/Media/apiRoutes.php和Modules/Media/backendRoutes.php文件中。可以帮助我,告诉我如何管理我的路线这样?

pw136qt2

pw136qt21#

在Laravel的Route上的group()方法,可以接受文件名,所以我们可以这样做:

// web.php

Route::prefix('admin')
    ->group(base_path('routes/admin.php'));

// admin.php
Route::get('/', 'AdminController@index');
o7jaxewo

o7jaxewo2#

1.创建2个布线文件routes.web.phproutes.api.php
1.编辑RouteServiceProvider.php文件,使其与以下示例类似:

<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Routing\Router;

class RouteServiceProvider extends ServiceProvider
{

    /**
     * This namespace is applied to the controller routes in your routes file.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $webNamespace = 'App\Http\Controllers\Web';

    protected $apiNamespace = 'App\Http\Controllers\Api';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @param  \Illuminate\Routing\Router $router
     *
     * @return void
     */
    public function boot(Router $router)
    {
        //

        parent::boot($router);
    }

    /**
     * Define the routes for the application.
     *
     * @param  \Illuminate\Routing\Router $router
     *
     * @return void
     */
    public function map(Router $router)
    {

        /*
        |--------------------------------------------------------------------------
        | Web Router 
        |--------------------------------------------------------------------------
        */

        $router->group(['namespace' => $this->webNamespace], function ($router) {
            require app_path('Http/routes.web.php');
        });

        /*
        |--------------------------------------------------------------------------
        | Api Router 
        |--------------------------------------------------------------------------
        */

        $router->group(['namespace' => $this->apiNamespace], function ($router) {
            require app_path('Http/routes.api.php');
        });

    }
}

注意:您可以添加任意数量的工艺文件...

slmsl1lt

slmsl1lt3#

您可以在任何地方创建所需数量的路由文件,然后只需在主路由文件smth中使用它们,如:

Route::get('/', function () {
    return 'Hello World';
});

Route::post('foo/bar', function () {
    return 'Hello World';
});

require_once "../../myModule1/routes.php";
require_once "../../myModule2/routes.php"
require_once "some_other_folder/routes.php"

在此,您将以与main中相同的方式定义路由

yh2wf1be

yh2wf1be4#

您可以使用Request::is(),这样您的主routes.php文件将如下所示:

if(Request::is('frontend/*))
{
    require __DIR__.'/frontend_routes.php;
}

if(Request::is('admin/*))
{
    require __DIR__.'/admin_routes.php;
}

你可以阅读更多的here

pjngdqdw

pjngdqdw5#

以防有人还在追杀
https://ctf0.wordpress.com/2016/10/01/split-routes-into-categories-in-laravel/
1-打开routes/web/api.php并添加

foreach (File::allFiles(__DIR__ . '/Routes') as $route_file) {
  require $route_file->getPathname();
}

2-现在,在同一级别创建一个新文件夹,并将其命名为“Routes”
3-创建文件根据每个路线ex.用户,后,stuff等...并添加您的路线逻辑正常

wribegjk

wribegjk6#

您可以在服务提供程序中加载自定义路由文件。AsgardCMS也是这样做的,请参见加载后端路由的Core RoutingServiceProvider中的此方法:
https://github.com/AsgardCms/Core/blob/master/Providers/RoutingServiceProvider.php#L77
Laravel文档在软件包开发部分提供了一个简单的示例:
http://laravel.com/docs/5.1/packages#routing

bvk5enib

bvk5enib7#

拉拉威尔^6
在评论中解释

<?php
namespace App\Providers;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * This namespace is applied to your Custom controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $custom_namespace = 'App\Http\Controllers\Custom';

    /**
     * The path to the "home" route for your application.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

    // map new custom routes
        $this->mapCustomRoutes();
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api.php'));
    }

   /**
     * Define the "custom" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapCustomRoutes()
    {
           Route::middleware('web')
            ->namespace($this->custom_namespace)  //or change its custom_namespace to namespace if you don't need change directory of your controllers  
            ->group(base_path('routes/custom.php')); // change custom.php to your custom routers file name
    }
}
rqenqsqc

rqenqsqc8#

如果你只想创建一个自定义的路由文件,你可以很容易地添加你的自定义路由文件,并使用Web中间件名称注册。
编辑应用程序\提供程序\路由服务提供程序. php

public function map()
   {
    /** Insert this Method Name **/
        $this->methodicalness();
   }

   protected function methodicalness()
   {
       Route::middleware('web')
       ->namespace($this->namespace)
       ->group(base_path('routes/yourRouteName.php'));
   }

注意:这种方法也工作,即使文件是在文件夹内只是跟踪你的路线文件所在的位置。
快乐编码。

q9yhzks0

q9yhzks09#

我的简单快速的解决方案,多个路线文件。它的工作!
在web.php文件中,添加:

foreach (glob(__DIR__. '/*') as $router_files){
    (basename($router_files =='web.php')) ? : (require_once $router_files);
}

相关问题