I am stuck with laravel localization. I want locale optional in my routes. But issue is that if locale is not send i want en by default. If locale is not explicitly in URL. Laravel considers first parameter like category as Locale.
Route::group(["prefix"=>'{locale?}','middleware'=>'getLang'],function() {
Route::get('/',[WebsiteController::class,'get_homepage'])->name('homepage');
Route::get('{category}',[WebsiteController::class,'category_all_news'])->name('category_all_news')->middleware('getLang');
Route::get('{category}/{slug}',[WebsiteController::class,'get_a_news_blog'])->name('get_a_news_blog')->middleware('getLang');
});
How can i solve this ?
Thanks in advance.
You are able to define optional parameters in your routes, by simply adding a question mark before them
In your case, you can add a parameter for the locale before your category
Route::get('/',[WebsiteController::class,'get_homepage'])->name('homepage');
Route::get('{locale?}/{category}',[WebsiteController::class,'category_all_news'])->name('category_all_news')->middleware('getLang');
Route::get('{locale?}/{category}/{slug}',[WebsiteController::class,'get_a_news_blog'])->name('get_a_news_blog')->middleware('getLang');
When passing the locale variable in your method you can just define a default value for the parameter
https://laravel.com/docs/9.x/routing#parameters-optional-parameters
Related
I had searched for how to add parameters to resource route
Route::resource('posts','PostsController');
// became
Route::resource('posts/category.post','PostsController');
now , by the category.post I can declare additional parameters to all resource routes
but they are required , my question is how to make them optional ?
I tried something like this
Route::resource('posts/category?.post','PostsController');
to make the category parameter be an optional one , but that didn't work with me .
how I can do so ?
thank you .
You can try this, not sure though..
Route::resource('posts', 'PostsController')->except(['store' ]);
Route::post('posts/category', 'PostsController#store');
Resource route is not just a "route"
You may see it as a route group, but it is predefined and can easily be implemented when you have a normal resource controller
If you want to change the parameters you'll have to define the routes individually
Then you can make the parameters optional as needed
Route::post('/posts/category/{post?}, 'PostsController#store');
See the following docs
https://laravel.com/docs/7.x/routing#parameters-optional-parameters
In my Laravel application I was using the normal routes, such as GET, POST, PUT and all with the various controllers. But as the application progresses, the routes/api.php file is turning out quite bulky. So, I am changing it with the "Resource Controller" syntax, for cleaner and reduced code.
However, earlier I was able to use the "token" or "slug" parameters in the url to fetch the data, for example: show/{token} & show($token) or show/{slug} & show($slug), but now whenever I try these or any other parameter I get 404 not found error. Therefore I am stuck with the default "id" parameter only.
Earlier:
Route::get('/categories', 'CategoryController#index');
Route::get('/categories/{token}', 'CategoryController#show');
Now:
Route::resource('/categories', 'CategoryController');
Is there any way to change the default "id" parameter to any other ..?
For Resource Routing the route parameter is the resource name, first argument, in singular form.
Route::resource('categories', 'CategoryController');
Will give you a route parameter of category. You can see this by running php artisan route:list and looking at the routes that are defined.
If you want to use a different parameter name for a resource you can also do that by overriding the parameter:
Route::resource('categories', 'CategoryController')->parameters([
'categories' => 'something',
]);
Now the route parameter used would be something.
This only has to do with the route parameter name, it has nothing to do with how any binding would be resolved, unless you had an Explicit Route Model Binding defined specifically for a parameter name.
If you are using Implicit Route Model Bindings you would define on your Model itself what field is used for the binding via the getRouteKeyName method. [In the next version of Laravel you will be able to define the field used in the route definition itself.]
Laravel 6.x Docs - Controllers - Resource Controllers - Naming Resource Parameters
Laravel 6.x Docs - Routing - Model Bindings - Implicit Route Model Binding getRouteKeyName
This can also be achieved by changing the customizing the key. In here slug refers to a column in the Category model.
Route::resource('categories', 'CategoryController')->parameters([
'categories' => 'categories:slug',
]);
How can I pass default value in resource field without showing that field in form while creating and updating?
I know I can use withMeta(['value' => '1']) to pass default value but it will won't work if field is not visible on form.
also, I know that in the model I can use $attribute variable to pass default value but I only want to pass the default value if resource created is from nova.
I've had good success with a combination of these two packages:
https://github.com/inspheric/nova-defaultable-fields
https://github.com/MohmmedAshraf/nova-hidden-field
For example:
Text::make('Email From')
->default(env('MAIL_FROM_ADDRESS')),
and
HiddenField::make('created_by_id')
->hideFromDetail()
->hideFromIndex()
->default(Auth::user()->id),
I'm setting up a new route system.
Route::get('/{cat1Url}', 'CategoryController#showCat1')->name('showCat1');
Route::get('/{productUrl}', 'ProductController#showProduct')->name('showProduct');
My sef link is after "/"
But,
{{ route('showProduct',[$p->pr_url]) }}
This method not working with route name. Working only upside route.
I don't want use
"/cat/myVariable"
or
"/product/myVariable"
Can't I use route name to work this way?
What is the solution to this?
In this way, if you make a get request to /something the laravel you start from top of web.php file looking to a route that follows the pattern. Your both routes will follow that pattern, but the laravel will always, pass the first one to controller.
You have two options:
Put only one route, and inside the controller you switch to the appropriate function. But this isn't a great ideia, because this is the function of the Web.php.
Use the routes like the documentation recommend:
Route::get('/cat/{catId}', 'CategoryController#showCat')->name('showCat');
Route::get('/prod/{productId}', 'ProductController#showProduct')->name('showProduct');
and in Controller you make the appropriate handler of your Category or Product.
You will have to have a way to tell Laravel which url to be mapped to what otherwise it will always use the last defined route. So in your case calling /myVariable and /myVariable it will use the latest definition which is showProduct. The only other way is if you use regular expression to differentiate the variables. For example:
Route::get('/{cat1Url}', 'CategoryController#showCat1')
->name('showCat1')->where('cat1Url', 'cat-*');
Route::get('/{productUrl}', 'ProductController#showProduct')
->name('showProduct')->where('productUrl', 'prod-*');
This way your slugs need to start with what you define, but you cannot use just id as a numeric value for both.
Can I have two params in one url such as:
localhost/post/image/id/something/else/value
where id is a param and something is the value and where else is the param and value is the value.
In zend I can only get the first param via:
$this->_getParam('id');
This method doesn't work for the second parameter.
Any ideas?
It should be working fine. Make sure you application.ini and plugins are doing default routing.
Default Zend Route looks like this:
With Out module
domainName/controller/action/var1/value1/var2/value2
With module
domainName/module/controller/action/var1/value1/var2/value2
Check following link for default Zend routes:
http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.default-routes
use ? to separate the variables in the querystring
something like http://localhost/post.php?imgID=1&otherValue=othervalue