Laravel routing : nested Route::group - php

I know there are tickets here, which are talking about the similar scenario, but I can't find any answer that would satisfy my case.
I'm trying to do the following:
Route::group(
[
'prefix' => 'admin',
'namespace' => 'Admin'
],
function() {
Route::controller('/', 'LoginController');
Route::group(
[
'prefix' => '',
'before' => 'auth.admin'
],
function() {
Route::controller('page', 'PageController');
Route::controller('article', 'ArticleController');
}
);
}
);
When I call /admin I get the LoginController and it's getIndex() view, but when I call /admin/page - I get:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
Controller method not found.
I know you can nest Route::group calls, but it doesn't seem to be documented well anywhere how to achieve it. From my understanding, you have to have 'prefix' specified with each Route::group call - In the nested one I've just used blank string '' - as it doesn't require any additional prefix apart from the parent one. The encapsulated calls to controllers within the nested group require admin.auth filter - and that's the reason why I wanted to enclose them in the nested group - rather than specifying filter for each controller separately.
Any idea what needs to be done to make this scenario work?
Also - even if I change the code so that it calls controllers directly under the parent group like so:
Route::group(
[
'prefix' => 'admin',
'namespace' => 'Admin'
],
function() {
Route::controller('/', 'LoginController');
Route::controller('page', 'PageController');
Route::controller('article', 'ArticleController');
}
);
I seem to be getting the same error when I call /admin/page - PageController looks like this:
namespace Admin;
use BaseController;
use View;
class PageController extends BaseController {
public function getIndex() {
return View::make('Admin.page.index');
}
}

I'll just chime in to say that by being explicit in your routing - only using Route::get/post/delete etc and not Route::controller or Route::resource - you avoid this sort of problem and lots of others. Route::controller especially is considered bad practice.

Ok - after experimenting with it for a while the answer appears to be in the order that you put the calls within your group.
When I move call to Route::controller('/', 'LoginController'); after the nested group - then everything seem to work fine:
Route::group(
[
'prefix' => 'admin',
'namespace' => 'Admin'
],
function() {
Route::group(
[
'prefix' => '',
'before' => 'auth.admin'
],
function() {
Route::controller('page', 'PageController');
Route::controller('article', 'ArticleController');
}
);
Route::controller('/', 'LoginController');
}
);
It's a shame that such an important aspect isn't documented anywhere - nevertheless - it works!

Related

Admin route group with prefix, middleware and named routes

I have some named routes in a controller named VehicleController:
vehicle.index
vehicle.show
And then I have an admin section, where I have defined a route group with prefix and middleware. In this section I have a resource controller name AdminVehicleController to handle CRUD tasks for the Vehicle (not sure if this is best practice) with the following routes:
vehicle.index
vehicle.create
vehicle.store
...
However these named routes are conflicting. My routes web.php looks like this for now:
Route::get('vehicles', 'VehicleController#index')->name('vehicle.index');
Route::get('vehicle/{vehicle}', 'VehicleController#show')->name('vehicle.show');
Route::group(['prefix' => 'admin', 'middleware' => 'is.admin'], function () {
Route::get('/', 'AdminDashboardController#index');
Route::resource('vehicle', 'AdminVehicleController');
});
If I add 'name' => 'admin' to the Route::group() array, the route names will be adminvehicle.index and not admin.vehicle.index.
What is the correct way to combine all these parameters in the route?
Try to use as parameter for your admin group
Route::group(['prefix' => 'admin', 'middleware' => 'is.admin', 'as'=> 'admin.'], function () {
Route::get('/', 'AdminDashboardController#index')->name('dashboard');
Route::resource('vehicle', 'AdminVehicleController');
});
Reference Link
Supply a names array as part of the third parameter $options array, with each key being the resource controller method (index, store, edit, etc.), and the value being the name you want to give the route.
Route::resource('vehicle', 'AdminVehicleController', [
'names' => [
'index' => 'admin.vehicle.index',
// etc...
]
]);

Laravel Route Resource both GET and POST

I have this Route
Route::group([ 'middleware' => ['auth','lang']], function() {
// SETTINGS
Route::namespace( 'Settings' )->prefix( 'settings' )->group( function () {
// INDEX
Route::get( '/', 'SettingsController#index' );
// ACCOUNTS
Route::resource( 'accounts', 'AccountController', ['only' => ['index','store','edit','update']] );
// TAGS
Route::resource( 'tags', 'TagController', ['only' => ['index','destroy']] );
// PROFILE
Route::get('profile', 'ProfileController#index');
Route::post('profile', 'ProfileController#update');
});
Any way I can join the two PROFILE ones into one that is resource? Whenever I try using Route::resource( 'profile', 'ProfileController', ['only' => ['index','update']] ), it gives me an error that the method is not allowed - 405 (Method Not Allowed). I think it just doesn't find the update one? I am really not sure what might be the issue.
This is happening because in the case of resourceful controllers, a post would be defaulted to a store method, not update.
So you are posting to the store method, which is not defined, giving you the 403 method not allowed.
To solve this, either change your request to a PUT or change your code to Route::resource( 'profile', 'ProfileController', ['only' => ['index','store']] ) Keep in mind, if you do this, you have to move the contents of your update function to store.
For more information, checkout https://laravel.com/docs/5.5/controllers#resource-controllers

Resource route generating strange model name from an 'ax' ending model

I registered this resource:
Route::resource('order-item-paxes', 'OrderItemPaxController', ['except' => ['show', 'create', 'store']]);
The problem is that I cannot get the model in the controller doing this:
public function edit(OrderItemPax $order_item_pax)
{
$order_item_pax = OrderItemPax::find($id);
return view('production.order-item-paxes.edit', compact('order_item_pax'));
}
$order_item_pax->toArray() returns an empty Array.
I checked the routes through php artisan route:list and its returning something strange:
PUT|PATCH | production/order-item-paxes/{order_item_paxis}
It should be order_item_pax instead of order_item_paxis.
Any idea?
UPDATE
If I use $order_item_paxis in my controller it works. I've registered hundreds of Resources and I've always used the singular version of the name
You can tell Laravel to override the route parameters by including parameters array in the $options array (3rd param):
Route::resource('order-item-paxes', 'OrderItemPaxController', [
'except' => ['show', 'create', 'store'],
'parameters' => ['order-item-paxes' => 'order_item_pax']
]);
Hope this helps!

Access a URL Parameter in a Route Prefix from Middleware

I am struggling to access a route prefix parameter from my middleware.
Given this URL: http://www.example.com/api/v1/campaign/40/status, and the following route:
Route::group( [
'prefix' => 'api/v1'
], function()
{
Route::group( [
'prefix' => 'campaign/{campaign}',
'where' => [ 'campaign' => '[0-9]+' ],
'middleware' => [
'inject_campaign'
]
], function()
{
Route::get( 'status', 'CampaignController#getStatus' );
} );
} );
How do I access the campaign parameter (40 in the example URL) from my inject_campaign middleware? I have the middleware running fine but cannot work out how to access the parameter.
Calling $request->segments() in my middleware gives me the parts of the route but this seems a fragile way of accessing the data. What if the route changes?
You can do it using shorter syntax
You can use:
echo $request->route()->campaign;
or even shorter:
echo $request->campaign;
Got it!
$request->route()->getParameter('campaign')

Laravel 4, group prefix requires changing route names. Really?

So I've been working on an app that lived on the domain root and now has to work on /admin. So URLs like domain.com/[resource] should now be domain.com/admin/[resource]. I didn't thought this very well before since I assumed that this had to be a very easy fix on Laravel. After all, that's one of the main reasons for not hardcoding routes, right?
So my routes.php file looked something like:
Route::group(['before' => 'auth'], function() {
Route::resource('books', 'BooksController');
... more resources here ...
});
Going through the docs I found that 'prefix' => 'admin' would do the trick:
Route::group(['prefix' => 'admin', 'before' => 'auth'], function() {
Route::resource('books', 'BooksController');
... more resources here ...
});
But it turns out that every route name get's changed from books.{action} to admin.books.{action} which requires me to change the whole app. Regexing would be dangerous and doing it manually would be annoying. Laravel was supposed to help with this! Or am I missing something?
This is untested, but after looking on the documentation for resource controllers it seems as though you can manually set their names. I'm assuming Laravel automatically namespaces grouped resource controller routes to avoid name collision, but you can override this to avoid going back through the rest of your app (just beware of future name collision):
Route::resource(
'books',
'BooksController',
array(
'names' => array(
'index' => 'photo.index',
'create' => 'photo.create',
'store' => 'photo.store',
'show' => 'photo.show',
'edit' => 'photo.edit',
'update' => 'photo.update',
'destroy' => 'photo.destroy',
)
)
);
Shorter Method:
Just define a quick method at the top of your routes.php file to shorten up this repetitive task of creating an array of route names. Still not the greatest solution, but I believe its the only thing you can do with how Laravel has this set up.
function createRouteNames($resource) {
$names = array();
$types = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy'];
foreach($types as $type) {
$names[$type] = $resource . '.' . $type;
}
return $names;
}
Route::resource('books', 'BooksController', ['names' => createRouteNames('books')]);
Note: [] === array() and may not be supported on older PHP's, meaning you may need to replace them with the old syntax.

Categories