Issue
Making routes of various pages of a website in web.php makes it bulky and unstructured. So my question is is there any way to save it in separate files?
// 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');
Solution
Short answer
Yes, you can store routes in different files.
Expanded answer
1 - Create your new Route file
Create your new route file, for this example I'll name it users.php
and store there the related routes:
- routes/users.php
Route::get('/my-fancy-route', 'MyCoolController@anAwesomeFunction');
// and the rest of your code.
2 - Add your route file to the RouteServiceProvider
Add here a new method, i'll call it mapUserRoutes
:
- app/Providers/RouteServiceProvider.php
/**
* 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 - Add it to the map()
method
In the same file(RouteServiceProvider.php
), go to the top and add your new method inside the map()
function:
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
// some other mapping actions
$this->mapUserRoutes();
}
4 - Final steps
I'm not fully sure if this is neccesary but never hurts to do:
Stop your server (if runing)
do
php artisan config:clear
Start your server.
Answered By - Kenny Horna
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.