Laravel 8 : Route with parameter in Routegroup don't work - php

i've trouble with laravel localization and route in grouproute.
I try to access this url :
http://localhost/logiluxe/public/en/games_giveaways/1
My routes :
Route::group(['middleware' => ['setlocale'],'prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}']], function() {
Auth::routes();
Route::get('/games_giveaways/{id}', [GameGiveawayController::class, 'show'])->name('games_giveaways.show');
});
My middleware :
public function handle(Request $request, Closure $next) {
if(session()->has('locale')){
app()->setLocale(session()->get('locale'));
//Carbon::setLocale(session()->get('locale'));
} elseif ($request->segment(1)) {
app()->setLocale($request->segment(1));
} else {
app()->setLocale('en');
}
return $next($request);
}
The error :
Missing required parameter for [Route: games_giveaways.show] [URI: {locale}/games_giveaways/{id}]
[Missing parameter: id]. (View: C:\wamp64\www\logiluxe\resources\views\layouts\app.blade.php)
It's look like i can't have a route with parameter in a routegroup.

Your error is in the balde, not the route declaration itself.
Somewhere in your blade you are calling the route by alias without the ID parameter.
//change this in your blade
route('games_giveaways.show', ['locale' => 'en']);
//to
route('games_giveaways.show', ['locale' => 'en', 'id' => 1]);

Related

Adding prefix as a variable in route::group in laravel

I got this error message when i am trying to add prefix as a variable in route group
error message :
UrlGenerationException in UrlGenerationException.php line 17:
Missing required parameters for [Route: client.login] [URI: login].
web.php :
Route::group(['prefix' => '{account}'], function()
{
Route::GET('login', ['as' => 'client.login', 'uses' => 'Client\Auth\LoginController#showLoginForm']);
}
controller :
public function showLoginForm()
{
return view('client.auth.login', $this->data);
}
Your controller function must accept as arguments the URL params defined in the routes.
In this case it should be
public function showLoginForm($account)
{
return view('client.auth.login', $this->data);
}
But you probably want to do something with account

how to add optional parameter to laravel named routes?

Using Laravel 4.2 and according to routing documentation
We can define a named route as
Route::get('user/profile', array('as' => 'profile', 'uses' => 'UserController#showProfile'));
And define an optional parameter with this other way
Route::get('user/{name?}', function($name = null)
{
return $name;
});
I want to add an optional parameter to a named route. How to combine both ?
Try this
Route::get('user/{name?}', function($name = null)
{
return $name;
})->name('foo');
Update
sorry the name method not exists in Laravel 4.2
You can do it in another way
Route::get('user/profile/{name?}', array('as' => 'profile', 'uses' => 'UserController#showProfile'))
or
Route::get('user/profile/{name?}', array('as' => 'profile', function($name = null) {
// your code here
})
You can define route common for all functions..
like:
Route::controller('uses', 'UserController');
And define function with optional parameters:
public function getView($param = 0)
//your code here
}
using this you can use optional parameters in a function on which you required.with help of ajax call on function.

L5.3 - Call route from another route

I've a simple route into the file web.php:
Route::get('first/{param?}', [
'uses' => 'App\Http\Controllers\MyController#index',
'as' => 'myControllerIndex'
]);
Now, I'd like to create a second route that uses the first route but passing specific params.
I tried something like this:
Route::get('second', function () {
return file_get_contents(route('myControllerIndex', ['param' => 'book1']));
});
but it doesn't work.
Can anyone help me?
Thank you.
You could use a redirect
Route::get('second', function () {
return redirect()->route('myControllerIndex', ['param' => 'book1']);
});
Or you can access the controller directly
Route::get('second', function () {
return app('App\Http\Controllers\MyController')->index('book1');
});

Multiple parameter to route error laravel

I have method in my controller (singleProduct):
public function singleProduct($slug)
{
$product= Product::where('slug','=', $slug)->first();
return view('public.product.show')->withProduct($product);
}
And my route is:
Route::get('/{category}/{slug}',['as' => 'single.product', 'uses' => 'LinkController#singleProduct']);
My code in view:
{{$product->title}}
Though i have passed both required parameter for route.My route is returning an error of:
Missing required parameters for [Route: single.product] [URI: {category}/{slug}].
The correct way for defining route params is like:
route('single.product', ['category' => $product->category, 'slug' => $product->slug])
So your route in view will be as:
{{$product->title}}
Docs
In route definition you have slug and category, but in method you get actually just the slug, so maybe adding $category to singleProduct will help:
public function singleProduct($category, $slug)
{
$product= Product::where('slug','=', $slug)->first();
return view('public.product.show')->withProduct($product);
}

How to pass extra parameters to controller from Laravel route

I'm trying to handle basic validation of my API calls in the Laravel's routes. Here is what I want to achieve:
Route::group(['prefix' => 'api/v1/properties/'], function () {
Route::get('purchased', 'PropertiesController#getPropertyByProgressStatus', function () {
//pass variable x = 1 to the controller
});
Route::get('waiting', 'PropertiesController#getPropertyByProgressStatus', function () {
//pass variable x = 2 to the controller
});
});
Long story short, depending on the segment of the URI after api/v1/properties/ I want to pass a different parameter to the controller. Is there a way to do that?
I was able to get it to work with the following route.php file:
Route::group(['prefix' => 'api/v1/properties/'], function () {
Route::get('purchased', [
'uses' => 'PropertiesController#getPropertyByProgressStatus', 'progressStatusId' => 1
]);
Route::get('remodeled', [
'uses' => 'PropertiesController#getPropertyByProgressStatus', 'progressStatusId' => 1
]);
Route::get('pending', [
'uses' => 'PropertiesController#getPropertyByProgressStatus', 'progressStatusId' => 3
]);
Route::get('available', [
'uses' => 'PropertiesController#getPropertyByProgressStatus', 'progressStatusId' => 4
]);
Route::get('unavailable', [
'uses' => 'PropertiesController#getPropertyByProgressStatus', 'progressStatusId' => 5
]);
});
and the following code in the controller:
public function getPropertyByProgressStatus(\Illuminate\Http\Request $request) {
$action = $request->route()->getAction();
print_r($action);
Pretty much the $action variable is going to let me access the extra parameter that I passed from the route.
I think that you can do it directly in the controller and receiving the value as a parameter of your route:
First you need to specify the name of the parameter in the controller.
Route::group(['prefix' => 'api/v1/properties/'], function ()
{
Route::get('{parameter}', PropertiesController#getPropertyByProgressStatus');
In this way, the getPropertyByProgressStatus method is going to receive this value, so in the controller:
class PropertiesController{
....
public function getPropertyByProgressStatus($parameter)
{
if($parameter === 'purchased')
{
//do what you need
}
elseif($parameter === 'waiting')
{
//Do another stuff
}
....
}
I hope it helps to solve your issue.
Take a view for this courses: Learn Laravel or Create a RESTful API with Laravel
Best wishes.
----------- Edited ---------------
You can redirect to the route that you want:
Route::group(['prefix' => 'api/v1/properties/'], function () {
Route::get('purchased', function () {
return redirect('/api/v1/properties/purchased/valueToSend');
});
Route::get('waiting', function () {
return redirect('/api/v1/properties/waiting/valueToSend');
});
Route::get('purchased/{valueToSend}', PropertiesController#getPropertyByProgressStatus);
});
Route::get('waiting/{valueToSend}', PropertiesController#getPropertyByProgressStatus);
});
});
The last two routes response to the redirections and send that value to the controller as a parameter, is the most near that I think to do this directly from the routes.

Categories