Add locale to laravel resource route - laravel

I have following route that expect locale in prefix
Route::group(['prefix' => '{locale}/admin', 'where' => ['locale' => '[a-zA-Z]{2}'], 'middleware' => 'setlocale'], function() {
Route::resource('/profile', 'Admin\ProfileController', ['except' => ['edit', 'create', 'destroy', 'show', 'store']]);
});
The final route supposed to be like this:
www.example.com/en/admin/profile
and
www.example.com/en/admin/profile/1
while I can get index route www.example.com/en/admin/profile, but I cannot get update route www.example.com/en/admin/profile/1 instead I have this
www.example.com/1/admin/profile/en
Here is how my routes are defined in blade
// Return index route correctly
{{route('profile.index', app()->getLocale())}}
//And
{{route('profile.update', [$user->id, app()->getLocale()])}}
// Return update route wrong as I mentioned above.
My question is: How should I define my update route to return correct url?

You are passing your parameters in wrong order. You need to send locale first and $user->id after that.
{{ route('profile.update', [app()->getLocale(), $user->id]) }}

Related

how group apiResource route and other routes together?

I am using apiResource and other routes. I grouped them like below:
Route::group(['prefix' => 'posts'], function () {
Route::group(['prefix' => '/{post}'], function () {
Route::put('lablabla', [PostController::class, 'lablabla']);
});
Route::apiResource('/', PostController::class, [
'names' => [
'store' => 'create_post',
'update' => 'edit_post',
]
]);
});
all apiResource routes except index and store do not work! How should I group routes?
Your syntax for routing is wrong,
Notes
You will provide a uri for the apiResource (plural)
eg. Route::apiResource('posts', PostController::class);
Your name of resource route is wrong
Get this out https://laravel.com/docs/8.x/controllers#restful-naming-resource-routes
it should be
Route::apiResource('posts', PostController::class)->names([
'store' => 'create_post',
'update' => 'edit_post',
]);
No need of repeating Route::group, you can just write your routes like this
Route::prefix('posts')->group(function () {
Route::put('lablabla', [PostController::class, 'lablabla']);
});
Route::apiResource('posts', PostController::class)->names([
'store' => 'create_post',
'update' => 'edit_post',
]);
Your syntax is incorrect, there is a names method. See the documentation here https://laravel.com/docs/8.x/controllers#restful-naming-resource-routes.

What does 'as' method do in Laravel

In the example from the tutorial, it shows up.
Route::group([
'prefix' => 'admin',
'as' => 'admin.'
], function () {}
Can someone tells me what 'as' does? Also, is the dot next to the 'admin' neccessary?
Thank you.
Let's say, for example, that you have this route:
Route::get('admin', [
'as' => 'admin', 'uses' => 'AdminController#index'
]);
By using as you assign custom name to your route. So now, Laravel will allow you to reference said route by using:
$route = route('admin');
So you don't have to build the URL manually over and over again in your code. You don't really need . notation if you only want to call your route admin. If you want a more detailed name of your route, lets say for ex. admin product route, then you use the . notation, like this:
Route::get('admin/product', [
'as' => 'admin.product', 'uses' => 'AdminController#showProduct'
]);
So now, you will be able to call this route by the assigned name:
$route = route('admin.product');
Update:
The previous answer I provided is valid for a single routes. For the route groups, the procedure is very similar. In the route groups you need the . notation when you add a custom name, since you will be referencing another route after that . notation. This will allow you to set a common route name prefix for all routes within the group. So by your example, lets say you have a dashboard route inside your admin route group:
Route::group(['as' => 'admin.'], function () {
Route::get('dashboard', ['as' => 'dashboard', function () {
//Some logic
}]);
});
Now, you will be able to call the dashboard route like this:
$route = route(admin.dashboard);
You can read more about this in Laravel official documentation.
you may specify an as keyword in the route group attribute array, allowing you to set a common route name prefix for all routes within the group.
For Example
Route::group(['as' => 'admin::'], function () {
// Route named "admin::"
});
UseRoute Name like {{route(admin::)}} or route('admin::')
you can use an 'as' as a named route. if you do not prefix your route name in group route than you may add custom route name like this.
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'roles'], 'roles' => ['2']], function () {
Route::post('/changeProfile', ['uses' => 'UserController#changeProfile',
'as' => 'changeProfile']);
});

How can I make index form resource route out of the middleware?

Now this route's without ( except ) it just find working but when I want to reach any route like index or store I must to login then get the data form them . So I want to make index route out of this middleware ( it's ok reach to index without login ) hope to got it :)
route's :
Route::group(['middleware' => 'auth:api'], function() {
Route::resource('cards', 'cardsController', ['except' => 'index']);
Route::resource('services', 'servicesController', ['except' => 'index']);
Route::get('getUserinfo', 'LoginController#getUser');
});
and this my route list for cards :
and this when I get all cards form postman :
You have ['except' => 'index'] in the route group. Try to take it out and check if it is working.
Edited:
if you want index showing without login, try writting another route outside of the route group
so something like this
Route::get('cards', 'cardsController#index');
Route::get('services', 'servicesController#index');
Route::group(['middleware' => 'auth:api'], function() {
Route::resource('cards', 'cardsController', ['except' => 'index']);
Route::resource('services', 'servicesController', ['except' => 'index']);
Route::get('getUserinfo', 'LoginController#getUser');
});
First you should name your Controllers properly, instead of
cardsController
name it to
CardsController
Your error occurs because you are excepting the index action in your Controller.
Instead of this:
Route::resource('cards', 'cardsController', ['except' => 'index']);
Route::resource('services', 'servicesController', ['except' => 'index']);
Do this:
Route::resource('cards', 'cardsController');
Route::resource('services', 'servicesController');
Check out the docs
Dont forget to name your Controller properly.
As you should define your route outside the middleware.... As u want to use it without login....
Route::get('cards', 'cardsController#index');
Route::get('services', 'servicesController#index');
In middleware u have to define the route as below
Route::resource('cards', 'cardsController')->except('index');
Route::resource('services', 'servicesController')->except('index');

How to check domain in laravel for some routes?

I need check domain for some routes. Then after check, I need redirect domain https://two.com to https://one.com with middleware in laravel.
For example:
Routes:
Route::get('/', ['as' => 'index', 'uses' => 'IndexController#index']);
Route::get('termsAndCondition', ['as' => 'termsAndCondition', 'uses' => 'IndexController#termsAndCondition']);
Route::get('aboutUs', ['as' => 'aboutUs', 'uses' => 'IndexController#aboutUs']);
Route::get('privacy', ['as' => 'privacy', 'uses' => 'IndexController#privacy']);
I need check aboutUs and privacy for domain name.
If domain name is https://two.com/aboutUs or https://two.com/privacy redirect to https://one.com/aboutUs or https://one.com/privacy.
I need check with middleware.
Thanks.
You can do something like this in a middleware:
if (request()->getHttpHost() === 'two.com' && in_array(request()->path(), ['aboutUs', 'privacy'])) {
return redirect('https://one.com/' . request()->path());
}
you can check url segments using segment() helper something like:
if(Request::segment(1)==='aboutus')
{
return redirect(route('your other route');
}
if(Request::segment(1)==='privacy')
{
return redirect(route('your other route');
}
You can add that check in your middleware, segment() expects an integer param like in case above 1 will check first wildcard after domain name.

LARAVEL: POST and GET routes with same name scrambling link_to_route

I got these routes:
Route::get('prefix/list/{page?}/{size?}', ['as' => 'prefix.list', 'uses' => 'MyController#getList']);
Route::post('prefix/list', ['as' => 'prefix.list', 'uses' => 'MyController#postList']);
When I call link_to_route() like so:
{{ link_to_route('prefix.list', $page, ['page' => $page, 'size' => $size]) }}
It creates this link:
http://my.site/prefix/list?page=5&size=12
But when I remove the post route, it renders correctly this:
http://my.site/prefix/list/5/12
I don't want to change the name of the routes because my system depends on them being the same. How can I solve this?
You could try just changing the order of the routes in your routes file, so that the get one comes last and overrides the post for the purposes of link_to_route().

Resources