So I writing a simple app that has nested resources: posts and comments. Naturally, posts are parents, comments are children, related each one to one post. Simple enough. Now I want comments index (and creation, POST) page to live at posts/{post_id}/comments, update (PUT) to live at /posts/{post_id}/comments/{comment_id}. So I tried this:
Route::resource('posts', 'PostsController');
Route::group(array('prefix' => 'posts/{post_id}'), function() {
Route::resource('comments', 'CommentsController');
});
But it won't work since the route name is registered as posts.{post_id}.comments.create. Basically the post_id placeholder is counted as part of the route and it's not neat. Any way of doing this nicely or should I just write routes one by one and get rid of the group/Route::resource/prefix thing?
You can use nested resources like this;
Route::resource('posts', 'PostsController');
Route::resource('posts.comments', 'CommentsController');
When you php artisan routes you will see your new routes;
+--------+-------------------------------------------------+------------------------+------------------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+-------------------------------------------------+------------------------+------------------------------------+----------------+---------------+
| | GET|HEAD posts | posts.index | PostsController#index | | |
| | GET|HEAD posts/create | posts.create | PostsController#create | | |
| | POST posts | posts.store | PostsController#store | | |
| | GET|HEAD posts/{posts} | posts.show | PostsController#show | | |
| | GET|HEAD posts/{posts}/edit | posts.edit | PostsController#edit | | |
| | PUT posts/{posts} | posts.update | PostsController#update | | |
| | PATCH posts/{posts} | | PostsController#update | | |
| | DELETE posts/{posts} | posts.destroy | PostsController#destroy | | |
| | GET|HEAD posts/{posts}/comments | posts.comments.index | CommentsController#index | | |
| | GET|HEAD posts/{posts}/comments/create | posts.comments.create | CommentsController#create | | |
| | POST posts/{posts}/comments | posts.comments.store | CommentsController#store | | |
| | GET|HEAD posts/{posts}/comments/{comments} | posts.comments.show | CommentsController#show | | |
| | GET|HEAD posts/{posts}/comments/{comments}/edit | posts.comments.edit | CommentsController#edit | | |
| | PUT posts/{posts}/comments/{comments} | posts.comments.update | CommentsController#update | | |
| | PATCH posts/{posts}/comments/{comments} | | CommentsController#update | | |
| | DELETE posts/{posts}/comments/{comments} | posts.comments.destroy | CommentsController#destroy | | |
+--------+-------------------------------------------------+------------------------+------------------------------------+----------------+---------------+
Related
I've built a website using Laravel that I'm happy with, however, due to not planning ahead I have built the admin/management panel, and now that I have to go through and do the user front end I'm wondering how I should have though to handle this.
Currently I have a web.php file containing lines like this:
# WEDDING HANDLING
Route::get('/admin/weddings', 'WeddingController#index');
Route::get('/admin/wedding/create', 'WeddingController#create');
Route::get('/admin/wedding/{wedding}', 'WeddingController#show');
Route::get('/admin/wedding/{wedding}/edit', 'WeddingController#edit');
Route::post('/admin/wedding/create', 'WeddingController#store');
Route::put('/admin/wedding/{wedding}/edit', 'WeddingController#update');
Route::delete('/admin/wedding/{wedding}/destroy', 'WeddingController#destroy');
# MENU HANDLING
Route::get('/admin/wedding/{wedding}/menus', 'MenuController#index');
Route::get('/admin/wedding/{wedding}/menu/create', 'MenuController#create');
Route::get('/admin/wedding/{wedding}/menu/{menu}', 'MenuController#show');
Route::get('/admin/wedding/{wedding}/menu/{menu}/edit', 'MenuController#edit');
Route::post('/admin/wedding/{wedding}/menu/create', 'MenuController#store');
Route::put('/admin/wedding/{wedding}/menu/{menu}/edit', 'MenuController#update');
Route::delete('/admin/wedding/{wedding}/menu/{menu}/delete', 'MenuController#destroy');
...continues
So from that you can probably see that I have multiple controllers that handle this for the admin, however, if a user logs in, they should be able to view the menus against the wedding, which I can stack with #auth tags, and seperate it out like that, however I'm afraid I'm going to end up with a web.php that looks like the following:
# WEDDING HANDLING
Route::get('/admin/weddings', 'WeddingController#index');
Route::get('/weddings', 'WeddingController#index');
...continues
Hopefully from my demo you can see what I'm asking.
tl;dr: How would you go about seperating User/Management areas without duplicating a ton of code.
You would need to create different controllers just to handle the front-end, like: FrontEndMenuController#yourFunction
you could also separate your admin routes into a group with the prefix 'admin' instead of repeating it on all the routes:
Route::prefix('admin')->group(function () {
Route::get('/wedding/{wedding}/menus', 'MenuController#index');
Route::get('/wedding/{wedding}/menu/create', 'MenuController#create');
Route::get('/wedding/{wedding}/menu/{menu}', 'MenuController#show');
Route::get('/wedding/{wedding}/menu/{menu}/edit', 'MenuController#edit');
});
And then have your front-end routes like that:
Route::get('/wedding/{wedding}/menu', 'FrontEndMenuController#index');
As you are almost adhering to the CRUD/REST convention, an addition to Adriano Marra's answer would be to also use resource controllers.
From Laravel documentation about resource controllers:
Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code.
Introducing Resource Controllers
In your use case your resource are the weddings, so you could rewrite your web.php routes as:
Route::prefix('admin')->group(function () {
Route::resource('weddings', 'WeddingsController');
});
These lines would register such routes:
+--------+-----------+-------------------------------+------------------+-------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+-------------------------------+------------------+-------------------------------------------------+------------+
| | GET|HEAD | admin/weddings | weddings.index | App\Http\Controllers\WeddingsController#index | web |
| | POST | admin/weddings | weddings.store | App\Http\Controllers\WeddingsController#store | web |
| | GET|HEAD | admin/weddings/create | weddings.create | App\Http\Controllers\WeddingsController#create | web |
| | GET|HEAD | admin/weddings/{wedding} | weddings.show | App\Http\Controllers\WeddingsController#show | web |
| | PUT|PATCH | admin/weddings/{wedding} | weddings.update | App\Http\Controllers\WeddingsController#update | web |
| | DELETE | admin/weddings/{wedding} | weddings.destroy | App\Http\Controllers\WeddingsController#destroy | web |
| | GET|HEAD | admin/weddings/{wedding}/edit | weddings.edit | App\Http\Controllers\WeddingsController#edit | web |
+--------+-----------+-------------------------------+------------------+-------------------------------------------------+------------+
NOTE: There's a small difference as the Route::resource(...) method uses the plural resource name you provided to it (weddings) for all the seven routes, and the singular form for the parameter name (wedding).
You could then register the menus resource in the same way:
Route::prefix('admin')->group(function () {
Route::resource('weddings', 'WeddingsController');
// Personally, I would make 'menus' a top level resource but I will
// stick to your routing example for the rest of the answer.
Route::resource('weddings/{wedding}/menus', 'WeddingMenusController');
});
Named Group Resource Routes
Furthermore you could also register the frontend wedding controller as a resource
Route::prefix('admin')->group(function () {
Route::resource('weddings', 'WeddingsController');
Route::resource('weddings/{wedding}/menus', 'WeddingMenusController');
});
// This will map only two methods for this resource.
Route::resource('weddings', 'FrontendController')->only(['index', 'show']);
The routes registered by your application would be:
+--------+-----------+--------------------------------------------+------------------+-------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+--------------------------------------------+------------------+-------------------------------------------------+------------+
| | GET|HEAD | admin/weddings | weddings.index | App\Http\Controllers\WeddingsController#index | web |
| | POST | admin/weddings | weddings.store | App\Http\Controllers\WeddingsController#store | web |
| | GET|HEAD | admin/weddings/create | weddings.create | App\Http\Controllers\WeddingsController#create | web |
| | GET|HEAD | admin/weddings/{wedding} | weddings.show | App\Http\Controllers\WeddingsController#show | web |
| | PUT|PATCH | admin/weddings/{wedding} | weddings.update | App\Http\Controllers\WeddingsController#update | web |
| | DELETE | admin/weddings/{wedding} | weddings.destroy | App\Http\Controllers\WeddingsController#destroy | web |
| | GET|HEAD | admin/weddings/{wedding}/edit | weddings.edit | App\Http\Controllers\WeddingsController#edit | web |
| | GET|HEAD | admin/weddings/{wedding}/menus | menus.index | App\Http\Controllers\WeddingsController#index | web |
| | POST | admin/weddings/{wedding}/menus | menus.store | App\Http\Controllers\WeddingsController#store | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/create | menus.create | App\Http\Controllers\WeddingsController#create | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/{menu} | menus.show | App\Http\Controllers\WeddingsController#show | web |
| | PUT|PATCH | admin/weddings/{wedding}/menus/{menu} | menus.update | App\Http\Controllers\WeddingsController#update | web |
| | DELETE | admin/weddings/{wedding}/menus/{menu} | menus.destroy | App\Http\Controllers\WeddingsController#destroy | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/{menu}/edit | menus.edit | App\Http\Controllers\WeddingsController#edit | web |
| | GET|HEAD | weddings | weddings.index | App\Http\Controllers\FrontendController#index | web |
| | GET|HEAD | weddings/{wedding} | weddings.show | App\Http\Controllers\FrontendController#show | web |
+--------+-----------+--------------------------------------------+------------------+-------------------------------------------------+------------+
If you look close at the above table, you will notice that the name column has some duplicate values. This would create conflict when you need to reference these particular routes by name anywhere in your application.
So you could solve this by prefixing the admin group's named routes with a custom prefix:
Route::prefix('admin')->name('admin.')->group(function () {
Route::resource('weddings', 'WeddingsController');
Route::resource('weddings/{wedding}/menus', 'WeddingMenusController');
});
// This will map only two methods for this resource.
Route::resource('weddings', 'FrontendController')->only(['index', 'show']);
This would solve any route conflict, as the administation routes has been correctly prefixed with admin. name:
+--------+-----------+--------------------------------------------+------------------------+-------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+--------------------------------------------+------------------------+-------------------------------------------------+------------+
| | GET|HEAD | admin/weddings | admin.weddings.index | App\Http\Controllers\WeddingsController#index | web |
| | POST | admin/weddings | admin.weddings.store | App\Http\Controllers\WeddingsController#store | web |
| | GET|HEAD | admin/weddings/create | admin.weddings.create | App\Http\Controllers\WeddingsController#create | web |
| | GET|HEAD | admin/weddings/{wedding} | admin.weddings.show | App\Http\Controllers\WeddingsController#show | web |
| | PUT|PATCH | admin/weddings/{wedding} | admin.weddings.update | App\Http\Controllers\WeddingsController#update | web |
| | DELETE | admin/weddings/{wedding} | admin.weddings.destroy | App\Http\Controllers\WeddingsController#destroy | web |
| | GET|HEAD | admin/weddings/{wedding}/edit | admin.weddings.edit | App\Http\Controllers\WeddingsController#edit | web |
| | GET|HEAD | admin/weddings/{wedding}/menus | admin.menus.index | App\Http\Controllers\WeddingsController#index | web |
| | POST | admin/weddings/{wedding}/menus | admin.menus.store | App\Http\Controllers\WeddingsController#store | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/create | admin.menus.create | App\Http\Controllers\WeddingsController#create | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/{menu} | admin.menus.show | App\Http\Controllers\WeddingsController#show | web |
| | PUT|PATCH | admin/weddings/{wedding}/menus/{menu} | admin.menus.update | App\Http\Controllers\WeddingsController#update | web |
| | DELETE | admin/weddings/{wedding}/menus/{menu} | admin.menus.destroy | App\Http\Controllers\WeddingsController#destroy | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/{menu}/edit | admin.menus.edit | App\Http\Controllers\WeddingsController#edit | web |
| | GET|HEAD | weddings | weddings.index | App\Http\Controllers\FrontendController#index | web |
| | GET|HEAD | weddings/{wedding} | weddings.show | App\Http\Controllers\FrontendController#show | web |
+--------+-----------+--------------------------------------------+------------------------+-------------------------------------------------+------------+
Namespacing Groups
Finally, you could go further with optimizations with introduction of namespaces (useful if you start to have more and more controllers).
You can create a folder in you app/Http/Controllers, for example Administration, where you store all of your administrative controllers.
In the web.php file, you just have to tell Laravel that the admin prefixed route group should look for controllers in the newly created folder:
Route::prefix('admin')->namespace('Administration')->name('admin.')->group(function () {
Route::resource('weddings', 'WeddingsController');
Route::resource('weddings/{wedding}/menus', 'WeddingMenusController');
});
// This will map only two methods for this resource.
Route::resource('weddings', 'FrontendController')->only(['index', 'show']);
This would lead to register these routes:
+--------+-----------+--------------------------------------------+------------------------+----------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+--------------------------------------------+------------------------+----------------------------------------------------------------+------------+
| | GET|HEAD | admin/weddings | admin.weddings.index | App\Http\Controllers\Administration\WeddingsController#index | web |
| | POST | admin/weddings | admin.weddings.store | App\Http\Controllers\Administration\WeddingsController#store | web |
| | GET|HEAD | admin/weddings/create | admin.weddings.create | App\Http\Controllers\Administration\WeddingsController#create | web |
| | GET|HEAD | admin/weddings/{wedding} | admin.weddings.show | App\Http\Controllers\Administration\WeddingsController#show | web |
| | PUT|PATCH | admin/weddings/{wedding} | admin.weddings.update | App\Http\Controllers\Administration\WeddingsController#update | web |
| | DELETE | admin/weddings/{wedding} | admin.weddings.destroy | App\Http\Controllers\Administration\WeddingsController#destroy | web |
| | GET|HEAD | admin/weddings/{wedding}/edit | admin.weddings.edit | App\Http\Controllers\Administration\WeddingsController#edit | web |
| | GET|HEAD | admin/weddings/{wedding}/menus | admin.menus.index | App\Http\Controllers\Administration\WeddingsController#index | web |
| | POST | admin/weddings/{wedding}/menus | admin.menus.store | App\Http\Controllers\Administration\WeddingsController#store | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/create | admin.menus.create | App\Http\Controllers\Administration\WeddingsController#create | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/{menu} | admin.menus.show | App\Http\Controllers\Administration\WeddingsController#show | web |
| | PUT|PATCH | admin/weddings/{wedding}/menus/{menu} | admin.menus.update | App\Http\Controllers\Administration\WeddingsController#update | web |
| | DELETE | admin/weddings/{wedding}/menus/{menu} | admin.menus.destroy | App\Http\Controllers\Administration\WeddingsController#destroy | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/{menu}/edit | admin.menus.edit | App\Http\Controllers\Administration\WeddingsController#edit | web |
| | GET|HEAD | weddings | weddings.index | App\Http\Controllers\FrontendController#index | web |
| | GET|HEAD | weddings/{wedding} | weddings.show | App\Http\Controllers\FrontendController#show | web |
+--------+-----------+--------------------------------------------+------------------------+----------------------------------------------------------------+------------+
You can create a new FrontEndController, and route any front end pages to that controller.
Route::get('/admin/weddings', 'WeddingController#index');
Route::get('/weddings', 'FrontEndControllerController#showWeddings');
Then you can use middlewear to diferentiate and refactor any repeated code into the Wedding model and just call that.
This is what i do when i need to separate admin and front end routes
Route::group([
'prefix' => 'admin',
'namespace' => 'Admin', // assumed
// 'as' => 'admin.'
// 'middleware' => 'admin.'
], function () {
Route::get('weddings', 'WeddingController#index');
Route::group([
'prefix' => 'wedding',
], function () {
// Wedding Management
Route::get('{wedding}/show', 'WeddingController#show'); // Updated with /show in url otherwise it will overlap create
Route::get('{wedding}/edit', 'WeddingController#edit');
Route::put('{wedding}/edit', 'WeddingController#update');
Route::delete('{wedding}/destroy', 'WeddingController#destroy');
Route::get('create', 'WeddingController#create');
Route::post('create', 'WeddingController#store');
// Here you differentiate WeddingController and MenuController
// Route::group([
// 'middleware' => 'only_applied_to_this_group',
// ], function () {
// Menu Management
Route::get('{wedding}/menus', 'MenuController#index');
Route::get('{wedding}/menu/create', 'MenuController#create');
Route::get('{wedding}/menu/{menu}', 'MenuController#show');
Route::get('{wedding}/menu/{menu}/edit', 'MenuController#edit');
Route::post('{wedding}/menu/create', 'MenuController#store');
Route::put('{wedding}/menu/{menu}/edit', 'MenuController#update');
Route::delete('{wedding}/menu/{menu}/delete', 'MenuController#destroy')
// });
});
});
Route::group([
'namespace' => 'Front',
'as' => 'front.'
], function () {
// Front-end Management
});
I tried to make it more simplified for better routes understanding, i hope this works.
Should a redirect with flash data persist the flash data if the auth middleware is involved?
A few housekeeping things to note that will answer some possible followup questions:
I am calling the web middleware.
I'm using the file sessions driver.
I can retrieve values stored in the session with the exception of flashed data.
I have tried reflashing the flashed data by adding the following line to the Authenticate middleware:
$request->session()->reflash();
As such, Authenticate.php now appears as follows:
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
$request->session()->reflash();
return $next($request);
}
This issue is also affecting the auth boilerplate generated by make:auth, resulting in the $errors failing to display on error.
UPDATE (3/29 # 08:54 EST)
I uncovered what I believe to have been the root cause, as you will see below. Each route was calling 'web' middleware twice. As such, two requests were actually taking place which was removing the flash message(s) before the user had a chance to see them. Original route:list is below.
+--------+----------+-------------------------+------+-----------------------------------------------------------------+---------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+---------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | groups | | App\Http\Controllers\GroupsController#index | web,web,auth |
| | GET|HEAD | groups/set-default/{id} | | App\Http\Controllers\GroupsController#setDefaultGroup | web,web,auth |
| | GET|HEAD | home | | App\Http\Controllers\HomeController#index | web,web,auth |
| | GET|HEAD | login | | App\Http\Controllers\Auth\AuthController#showLoginForm | web,web,guest |
| | POST | login | | App\Http\Controllers\Auth\AuthController#login | web,web,guest |
| | GET|HEAD | logout | | App\Http\Controllers\Auth\AuthController#logout | web,web |
| | POST | password/email | | App\Http\Controllers\Auth\PasswordController#sendResetLinkEmail | web,web,guest |
| | POST | password/reset | | App\Http\Controllers\Auth\PasswordController#reset | web,web,guest |
| | GET|HEAD | password/reset/{token?} | | App\Http\Controllers\Auth\PasswordController#showResetForm | web,web,guest |
| | GET|HEAD | register | | App\Http\Controllers\Auth\AuthController#showRegistrationForm | web,web,guest |
| | POST | register | | App\Http\Controllers\Auth\AuthController#register | web,web,guest |
| | GET|HEAD | visitees | | App\Http\Controllers\VisiteesController#index | web,web,auth |
| | GET|HEAD | visitees/check-in/{id} | | App\Http\Controllers\VisiteesController#checkIn | web,web,auth |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+---------------+
My routes now appear as follows after removing the routes from the 'web' middleware:
+--------+----------+-------------------------+------+-----------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | groups | | App\Http\Controllers\GroupsController#index | web,auth |
| | GET|HEAD | groups/set-default/{id} | | App\Http\Controllers\GroupsController#setDefaultGroup | web,auth |
| | GET|HEAD | home | | App\Http\Controllers\HomeController#index | web,auth |
| | GET|HEAD | login | | App\Http\Controllers\Auth\AuthController#showLoginForm | web,guest |
| | POST | login | | App\Http\Controllers\Auth\AuthController#login | web,guest |
| | GET|HEAD | logout | | App\Http\Controllers\Auth\AuthController#logout | web |
| | POST | password/email | | App\Http\Controllers\Auth\PasswordController#sendResetLinkEmail | web,guest |
| | POST | password/reset | | App\Http\Controllers\Auth\PasswordController#reset | web,guest |
| | GET|HEAD | password/reset/{token?} | | App\Http\Controllers\Auth\PasswordController#showResetForm | web,guest |
| | GET|HEAD | register | | App\Http\Controllers\Auth\AuthController#showRegistrationForm | web,guest |
| | POST | register | | App\Http\Controllers\Auth\AuthController#register | web,guest |
| | GET|HEAD | visitees | | App\Http\Controllers\VisiteesController#index | web,auth |
| | GET|HEAD | visitees/check-in/{id} | | App\Http\Controllers\VisiteesController#checkIn | web,auth |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+------------+
Upon moving the routes out of the 'web' middleware group, the flash message display correctly. But, now I have a new issue!
The flash messages are not being removed from the session after the initial request. They persist each subsequent request until they are manually flushed or forgotten.
I'm not sure at this point if I should open up a second question that specifically addresses the persisting of the flash data. Please advise if so.
please run a composer update to update laravel/framework to v5.2.27, then issue php artisan make:auth to regenerate auth routes
I created a controller named CatController with php artisan make:controller CatController. So it generated the next route list:*-> What generated the route list is on routes.php Route::resource('cat','CatsController');
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-------------------+-------------+-------------------------------------------------+------------+
| | GET|HEAD | / | | Furbook\Http\Controllers\CatController#index | |
| | GET|HEAD | cat | cat.index | Furbook\Http\Controllers\CatController#index | |
| | POST | cat | cat.store | Furbook\Http\Controllers\CatController#store | |
| | GET|HEAD | cat/create | cat.create | Furbook\Http\Controllers\CatController#create | |
| | DELETE | cat/{cat} | cat.destroy | Furbook\Http\Controllers\CatController#destroy | |
| | PATCH | cat/{cat} | | Furbook\Http\Controllers\CatController#update | |
| | PUT | cat/{cat} | cat.update | Furbook\Http\Controllers\CatController#update | |
| | GET|HEAD | cat/{cat} | cat.show | Furbook\Http\Controllers\CatController#show | |
| | GET|HEAD | cat/{cat}/edit | cat.edit | Furbook\Http\Controllers\CatController#edit | |
Later on I thought it would be better to call it CatsController and handle the urls as cats/... so I renamed the controller but I still have the same default REST actions URIs.
Is there anyway to change it? How should I proceed?
To my knowledge the make:controller command only generates the controller file, not any route definitions. The routes defined there look like they've beed generated by a Route::resource definition that would look like this:
Route::resource('cat', 'CatController');
To make that work with cats and CatsControllers you should change it to this:
Route::resource('cats', 'CatsController');
You can read more on RESTful Resource Controllers in the Laravel Documentation.
I'm using Laravel Resource routing (through a controller). Here is the routing code
Route::resource( 'difficulty', 'DifficultyController', [ 'only' => [ 'index', 'show', 'update', 'create' ] ] );
Here are the routes created
+--------+----------+-------------------------+-------------------+--------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-------------------------+-------------------+--------------------------------------------------+------------+
| | GET|HEAD | difficulty | difficulty.index | App\Http\Controllers\DifficultyController#index | |
| | GET|HEAD | difficulty/create | difficulty.create | App\Http\Controllers\DifficultyController#create | |
| | PATCH | difficulty/{difficulty} | | App\Http\Controllers\DifficultyController#update | |
| | GET|HEAD | difficulty/{difficulty} | difficulty.show | App\Http\Controllers\DifficultyController#show | |
| | PUT | difficulty/{difficulty} | difficulty.update | App\Http\Controllers\DifficultyController#update | |
+--------+----------+-------------------------+-------------------+--------------------------------------------------+------------+
It works fine, except that I don't need the "HEAD" and "PATCH" methods and I want to remove them. So listing routes will display the following
+--------+----------+-------------------------+-------------------+--------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-------------------------+-------------------+--------------------------------------------------+------------+
| | GET | difficulty | difficulty.index | App\Http\Controllers\DifficultyController#index | |
| | GET | difficulty/create | difficulty.create | App\Http\Controllers\DifficultyController#create | |
| | GET | difficulty/{difficulty} | difficulty.show | App\Http\Controllers\DifficultyController#show | |
| | PUT | difficulty/{difficulty} | difficulty.update | App\Http\Controllers\DifficultyController#update | |
+--------+----------+-------------------------+-------------------+--------------------------------------------------+------------+
Is it possible to do it? I'm using Laravel 5.1
What about just doing explicit declarations?
Route::get('/difficulty','DifficultyController#index');
Route::get('/difficulty/create','DifficultyController#create');
Route::get('/difficulty/{difficulty}','DifficultyController#show');
Route::put('/difficulty/{difficulty}','DifficultyController#update');
I even prefer having it this way since it gives a clearer picture of what your application does.
I am working on a CMS of my own, but I'm experiencing some difficulties with slugs, so, here is my problem:
I'm trying to use slugs on the root of my URLs like so:
http://domain.com/some-slug-to-some-article
At this point I have the following entries in my app/routes.php
Route::get('/', 'ArticleController#index');
Route::get('/{slug?}', 'ArticleController#show');
The problem appears when I am try to enter to the "login" area of the site, which for default is in /home (I'm using the scaffold for user authentication in Laravel 5.1)
When I enter the URL for domain.com/home it stills calling it as a slug.
Here's my route:list
+--------+--------------------------------+-------------------------------------------------------+----------------------+------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+--------------------------------+-------------------------------------------------------+----------------------+------------------------------------------------------------+------------+
| | GET|HEAD | / | | App\Http\Controllers\ArticleController#index | |
| | POST | auth/login | | App\Http\Controllers\Auth\AuthController#postLogin | guest |
| | GET|HEAD | auth/login | | App\Http\Controllers\Auth\AuthController#getLogin | guest |
| | POST | auth/login/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\AuthController#postLogin | guest |
| | GET|HEAD | auth/login/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\AuthController#getLogin | guest |
| | GET|HEAD | auth/logout | | App\Http\Controllers\Auth\AuthController#getLogout | |
| | GET|HEAD | auth/logout/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\AuthController#getLogout | |
| | POST | auth/register | | App\Http\Controllers\Auth\AuthController#postRegister | guest |
| | GET|HEAD | auth/register | | App\Http\Controllers\Auth\AuthController#getRegister | guest |
| | GET|HEAD | auth/register/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\AuthController#getRegister | guest |
| | POST | auth/register/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\AuthController#postRegister | guest |
| | GET|HEAD|POST|PUT|PATCH|DELETE | auth/{_missing} | | App\Http\Controllers\Auth\AuthController#missingMethod | guest |
| | GET|HEAD | home | | \Bestmomo\Scafold\Http\Controllers\HomeController#index | auth |
| | POST | home/article | home.article.store | App\Http\Controllers\ArticleController#store | auth |
| | GET|HEAD | home/article | home.article.index | App\Http\Controllers\ArticleController#index | auth |
| | GET|HEAD | home/article/create | home.article.create | App\Http\Controllers\ArticleController#create | auth |
| | GET|HEAD | home/article/{article} | home.article.show | App\Http\Controllers\ArticleController#show | auth |
| | PUT | home/article/{article} | home.article.update | App\Http\Controllers\ArticleController#update | auth |
| | DELETE | home/article/{article} | home.article.destroy | App\Http\Controllers\ArticleController#destroy | auth |
| | PATCH | home/article/{article} | | App\Http\Controllers\ArticleController#update | auth |
| | GET|HEAD | home/article/{article}/edit | home.article.edit | App\Http\Controllers\ArticleController#edit | auth |
| | GET|HEAD | password/email/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\PasswordController#getEmail | guest |
| | POST | password/email/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\PasswordController#postEmail | guest |
| | POST | password/reset/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\PasswordController#postReset | guest |
| | GET|HEAD | password/reset/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\PasswordController#getReset | guest |
| | GET|HEAD|POST|PUT|PATCH|DELETE | password/{_missing} | | App\Http\Controllers\Auth\PasswordController#missingMethod | guest |
| | GET|HEAD | {slug?} | | App\Http\Controllers\ArticleController#show | |
+--------+--------------------------------+-------------------------------------------------------+----------------------+------------------------------------------------------------+-------------
So in short words, I want to have the slugs at the root of the URL but also, I want to "exclude" some routes to keep it to the system itself.
Thanks for your help.
For further requirements, I've solved the problem adding this rule at the beginning of the routes.php
Route::get('home', 'HomeController#index');
Also, #StuartWagner said that clearing the routes.php should be empty the routes this was not true at all, once cleared the routes.php this is what the php artisan route:list show me:
+--------+--------------------------------+-------------------------------------------------------+------+------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+--------------------------------+-------------------------------------------------------+------+------------------------------------------------------------+------------+
| | POST | auth/login/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\AuthController#postLogin | guest |
| | GET|HEAD | auth/login/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\AuthController#getLogin | guest |
| | GET|HEAD | auth/logout/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\AuthController#getLogout | |
| | GET|HEAD | auth/register/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\AuthController#getRegister | guest |
| | POST | auth/register/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\AuthController#postRegister | guest |
| | GET|HEAD|POST|PUT|PATCH|DELETE | auth/{_missing} | | App\Http\Controllers\Auth\AuthController#missingMethod | guest |
| | GET|HEAD | home | | \Bestmomo\Scafold\Http\Controllers\HomeController#index | auth |
| | POST | password/email/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\PasswordController#postEmail | guest |
| | GET|HEAD | password/email/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\PasswordController#getEmail | guest |
| | POST | password/reset/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\PasswordController#postReset | guest |
| | GET|HEAD | password/reset/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\PasswordController#getReset | guest |
| | GET|HEAD|POST|PUT|PATCH|DELETE | password/{_missing} | | App\Http\Controllers\Auth\PasswordController#missingMethod | guest |
+--------+--------------------------------+-------------------------------------------------------+------+------------------------------------------------------------+------------+
In some where is declared this route for "home".
Hope this helps anyone.
Cheers.