generate route for routes defined inside group - php

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 :)

Related

Laravel 8 how to name a group

This question has been asked but it's outdated in most answers I saw.
This is what I'm trying:
Route::prefix('cart')->group(function() {
Route::get('/mycart','App\Http\Controllers\frontend\CartController#mycart')->name('mycart');
Route::get('/checkout','App\Http\Controllers\frontend\CartController#checkout')->name('checkout');
})->name('cart');
I'd like to name the group and then in my blade be able to do:
#if (Route::currentRouteName() != 'individual_product')
The above gives me this error: Call to a member function name() on null
Any idea what i'm doing wrong?
One can configure everything with arrays:
use Illuminate\Support\Facades\Route;
Route::group(['prefix' => 'cart', 'name' => 'cart.'], function() {
Route::get('/index', ['as' => 'index', 'uses' => 'CartController#index']);
Route::get('/checkout', ['as' => 'checkout', 'uses' => 'CartController#checkout']);
});
And 'as' => 'cart.index' might still be more readable than a named group (it may depend).
In Blade one can also use Route:
#if (Route::has('cart')) #endif
#if (Route::has('cart.index')) #endif
Listing all defined routes is being supported:
php artisan route:list
You can use name prefixes like below:
Route::name('cart.')->prefix('cart')->group(function() {
Route::get('/mycart','App\Http\Controllers\frontend\CartController#mycart')->name('mycart');
Route::get('/checkout','App\Http\Controllers\frontend\CartController#checkout')->name('checkout');
});
for more : https://laravel.com/docs/8.x/routing#route-group-name-prefixes
in blade you can check
#if(request()->routeIs('cart.*')) // or ->routeIs('cart.mycart')
#endif
https://laravel.com/docs/8.x/requests#retrieving-the-request-path

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...
]
]);

Optional route parameters not working in Lumen 5.7

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}]

Laravel 5 named route with parameter

I'm pretty new on laravel5 and I'm trying to generate dynamically route alias under route.php
This is it:
Route::get('/menu/{category}/{product}/{item}', 'MenuController#listItem')->name('/{category}/{item}');
I already tried with with 'as' and 'uses' and I'm still getting:
/menu/{category}/{product}/{item}
With all parameters replaced by the correct values instead of:
/{category}/{item}
Expounding on what Vinicius Luiz said.
Route::get('/menu/{category}/{product}/{item}', ['as' => 'named.route' , 'uses' => 'MenuController#listItem']);
// to get the actual linke
route('named.route', ['category' => $category->id, 'product' => $product->id, 'item' => $item->id]);
depending, you may not do ->id or anything, you might just pass the whole $category, $product, etc. Depends on how the routing in your controllers is setup.
EDIT:
From your comment, it likes like you want something like:
class MenuController {
public function lisItem($category_name, $product_name) {
$category = Category::where('name', $category_name)->first(['id']);
$product = Product::where('category_id', $category->id)->where('name', $product_name')->first();
}
}
Route::get('/{category}/{item}', ['as' => 'named.route' , 'uses' => 'MenuController#listItem']);
// to get the actual linke
route('named.route', ['category' => $category->id, 'item' => $item->id]);
there is probably a better way to do the queries, but that should work for you.
Try it:
Route::get('/menu/{category}/{product}/{item}', ['as' => 'a.name.to.your.route' , 'uses' => 'MenuController#listItem']);

Named Routes Conflict Laravel 5.2

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 :)

Categories