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...
]
]);
Related
I had defined my route and controller as following
$router->group(['prefix' => 'api/v1'], function ($router) {
$router->group(
['middleware' => 'auth'], function() use ($router) {
$router->get('/order/get-order-status/{order_id}[/{account_id}]'
, [
'uses' => 'Api\V1\OrderController#getOrderStatus'
, 'as' => 'getOrderStatus'
]
);
});
});
following is the function defination
public function getOrderStatus($orderId, $accountId = false)
{
// my code goes here
}
Here the problem is whenever, I skip the optional account_id from the route, then passed order_id is assigned to the 2nd parameter of the function i,e. accountId. If I pass both params then everything is working as expected. I'm just confused whether something is wrong in my configuration or Lumen itself have some issue with optional route params?
Consider I had triggered http://localhost/lumen/api/v1/order/get-order-status/ORD1234 then ORD1234 is assigned to accountId and '0' is assigned to orderId
optional route parameters are given like below,
$router->get('/order/get-order-status/{order_id}/{account_id?}' // see the ? mark
though I am not sure why 0 is assigned to the orderId,
and usually, the first parameter to the controller method is a request object, so you can easily identify what are the things that the request contains.
public function getOrderStatus(Request $reqeust, $orderId, $accountId = false)
move out your optional parameter route outside the group
$router->group(['prefix' => 'api/v1'], function ($router) {
$router->get('/order/get-order-status/{order_id}[/{account_id}]'
, [
,'middleware' => 'auth',
'uses' => 'Api\V1\OrderController#getOrderStatus'
, 'as' => 'getOrderStatus'
]
);
or like this following code
$router->get('api/v1/order/get-order-status/{order_id}[/{account_id}]',
['uses' => 'Api\V1\OrderController#getOrderStatus',
'middleware' => 'auth',
'as' => 'getOrderStatus'
]
);
I think you should use optional parameter like
{account_id?} rather then [/{account_id}]
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
** I have Multiple Route in Laravel. I wanna pass common parameter for every Route i,e If i passed POST as a parameter in any route, I need to Call POST controller for all URL.
My Routes are below:
Original URL: www.mydomain.com/home/
Required URL: www.mydomain.com/home/post
Original URL: www.mydomain.com/follow/
Required URL: www.mydomain.com/follow/post
In above URL I have two separate blades like home.blade.php AND post.blade.php,
In Second Example I have two separate blades like follow.blade.php AND post.blade.php. Here Post.blade.php is common.
For Both home and follow must call postcontroller. My Route Controllers . **
//for follow Route
Route::get('Follow', [
'as' => 'Follow',
'uses' => 'PageController#getFollow'
]);
//for Home Route
Route::get('Home', [
'as' => 'Home',
'uses' => 'PageController#getHome'
]);
Post Route controller is below
Route::get('Post', [
'as' => 'Post',
'uses' => 'PostController#getPOST'
]);
I have this route defined inside a group
Route::group(['domain' => '{subdomain}.test.com'], function () {
Route::get('/models/{id?}', [
'as' => 'car-model',
'uses' => 'CarModelController#details'
]);
});
I want to avoid hardcoding URLs in blade
{{route('car-model', 'ford', '100) }}
but that returs this url
ford.test.com/models
no model id!
Not sure if is relevant but in my controller CarModelController.php
I defined
public function details($subdomain, $id)
why is not sending the id to the generated url? Do I need to send the $subdomain parameter to the detail function?
I found that
{{route('car-model', ['make' => 'ford', 'id' => '100]) }}
works! thanks for watching :)
I am creating a Multilingual Laravel 5.2 application and I am trying to figure out how to change language when I already have the content.
routes.php
...
Route::get('home', [
'as' => 'index',
'uses' => 'SiteController#home'
]);
Route::get([
'as' => 'index',
'uses' => 'SiteController#inicio'
]);
...
I have SiteController#home and SiteController#inicio. So I change session('language') in SiteController#change_language like:
...
public function change_language ($lang){
session(['language' => $lang]);
return redirect()->action(SAME NAMED ROUTE, DIFFERENT LANGUAGE);
}
...
So, When I click on a button with
English
from /inicio (SiteController#inicio) I should be redirected to the same named route (SiteController#home) so I can check the language and display appropriate content.
Any ideas of how to get the named route or something helpful?
Thank you :)