在 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');
Run Code Online (Sandbox Code Playgroud)
是的,您可以将路线存储在不同的文件中。
创建您的新路由文件,在本例中,我将命名它users.php并将相关路由存储在那里:
Route::get('/my-fancy-route', 'MyCoolController@anAwesomeFunction');
// and the rest of your code.
Run Code Online (Sandbox Code Playgroud)
在此处添加一个新方法,我将调用它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'));
}
Run Code Online (Sandbox Code Playgroud)
map()方法中在同一个文件(RouteServiceProvider.php)中,转到顶部并在map()函数中添加您的新方法:
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
// some other mapping actions
$this->mapUserRoutes();
}
Run Code Online (Sandbox Code Playgroud)
我不完全确定这是否是必要的,但这样做不会有什么坏处:
停止您的服务器(如果正在运行)
做 php artisan config:clear
启动你的服务器。