Laravel - 在web.php以外的文件中创建路由

时间:2018-02-23 06:13:28

标签: laravel laravel-5

在web.php中制作网站各个页面的路径会使其变得庞大且非结构化。所以我的问题是有没有办法将它保存在单独的文件中?

// Calling Registration function
Route::any('/admin-store','AdminUserController@store')->name('admin-reg'); 

// Redirecting to Registration page
Route::get('/admin-registration','AdminUserController@index')->name('admin-registration'); 

// Redirecting to login page
Route::get('/admin-login','AdminLoginController@index')->name('admin-login'); 

// Redirecting to Admin Profile Page
Route::get('/admin-profile','AdminProfileController@index')->name('admin-profile'); 

// Calling Login function
Route::post('admin-login-result','AdminLoginController@login')->name('admin-log'); 

// Calling Function to Update Profile
Route::post('admin-edit-profile','AdminEditController@updateProfile')
         ->name('admin-edit-profile'); 

// Redirecting to Profile Edit page
Route::get('/admin-edit-profile','AdminEditController@index')->name('admin-edit'); 

1 个答案:

答案 0 :(得分:1)

简短回答

是的,你可以。

长答案

1 - 创建新的路径文件

创建新的路由文件,对于此示例,我将其命名为users.php并在那里存储相关路由:

  

路由/ users.php

Route::get('/my-fancy-route', 'MyCoolController@anAwesomeFunction');
// and the rest of your code.

2 - 将您的路线文件添加到RouteServiceProvider

  

应用程序/提供者/ RouteServiceProvider.php

在这里添加一种新方法,我称之为mapUserRoutes

/**
 * Define the User routes of the application.
 *
 *
 * @return void
 */
protected function mapUserRoutes()
{
    Route::prefix('v1')  // if you need to specify a route prefix
        ->middleware('auth:api') // specify here your middlewares
        ->namespace($this->namespace) // leave it as is
        /** the name of your route goes here: */
        ->group(base_path('routes/users.php'));
}

3 - 将其添加到map()方法

在同一个文件(RouteServiceProvider.php)中,转到顶部并在map()函数中添加新方法:

/**
 * Define the routes for the application.
 *
 * @return void
 */
public function map()
{

    // some other mapping actions

    $this->mapUserRoutes();
}

4 - 最后步骤

我不完全确定这是否是必要的,但从来没有伤害过:

  • 停止服务器(如果正在运行)

  • 执行php artisan config:clear

  • 启动服务器。

相关问题