Multi-language site. Set routing's rules - laravel

I was created this simple routing
Route::group(['prefix' => '{lang}'], function(){
Route::get('/hello', function($lang){
App::setlocale($lang);
return view('welcome');
});
});
It works of course, but in this example I returned only view. I prefer returned controller's method so in my previous project I realize routing in this way:
Route:get('/hello', [
'uses' => 'MyController#myMethod',
'as' => 'myMethod'
]);
How can I use localization and returned controller's method.

You can implement it through the Accept-Language header or the parameter that will determine which language, for example /route/{language}. Next, send a request to the rout, which will call the method in the controller and already in the method to check - which language to use.
Another option is to create multiple routines for multiple languages. And ask for rows depending on the language. There is a minus, if many languages ​​are used, you need to create a lot of routes

Related

Laravel Language prefix with additional parameters

Quick question that is already killing me for days.
With Laravel I am trying to use different languages.
English and Japanese
This works in the route like this.
Route::group([
'prefix' => '{lang}',
'where' => ['lang' => '(jp|en)'],
'middleware' => 'Language'
], function() {
Route::get('/blogs', 'BlogController#index')->name('main-blog');
Route::get('/blog/{postId}/{postTitle}', 'BlogController#post');
});
This works when I am visiting the "/blogs" page.
It changes between languages.
Now when I visit the "/blog/{postId}/{postTitle}" page I can't access the posted parameter anymore within my controller.
Somehow it only shows the "lang" parameter.
What would be the right way to access a parameter when using a prefix.
When I don't use the prefix it works like a charm.
My Controller;
public function post($blog_id, $blog_title)
{
// Do something
}
Help is highly appreciated.
I have been banging my head with this for days now.
Wesley
You use the prefix parameter to specify common parameters for your grouped routes. So You need one more parameter $lang for this controller:
public function post($lang, $blog_id, $blog_title)
{
// Do something
}
With prefix parameter the routes look like these:
/{lang}/blogs
/{lang}/blog/{postId}/{postTitle}

Route User Role Laravel 5.4

I'm very confused on this situation. I have two routes with on resource name.
Route::resource('product', 'Product\AreaManagerProductController');
Route::resource('product', 'Product\SystemAdminProductController');
I need to make it as one because I have a contextual binding.
$this->app->when(AreaManagerProductController::class)
->needs(ProductInterface::class)
->give(AreaManagerProductRepository::class);
$this->app->when(SystemAdminProductController::class)
->needs(ProductInterface::class)
->give(SystemAdminProductRepository::class);
The contextual binding works fine... but I need to change my route like this.
Route::resource('product_area_manager', 'Product\AreaManagerProductController');
Route::resource('product_system_admin', 'Product\SystemAdminProductController');
I created ProductController and some kind of weird solution.
public function index(){
//Create a conditional statement base on user
return app('App\Http\Controllers\Product\AreaManagerProductController')->index();
}
It may work but it doesn't trigger the middleware... What could be the best practice on this situation. TY
You can have your Route like this
Route::group(['prefix' => 'product', 'namespace' => 'Product', 'middleware' => '<your middleware>'], function() {
Route::resource('area_manager', 'AreaManagerController');
Route::resource('system_admin', 'SystemAdminController');
});
The reason I grouped the route is to reduce redundancy, and the reason i removed Product from the controller name is, as there is a namespace Product already, there is no need of long Class names.
If you wan to access some methods in the AreaManagerController and SystemAdminController just extend the ProductController to these Controllers.
If you want to add some specific middleware for the actions inside these controllers, I have added a middleware clause in the route group which will affect to these controllers, if not needed just remove it.
Hope this helps you.

how to set common controller in group route

I am working on laravel but i have no idea about using route.
i used route group method but i have a question that can we use a common controller in group route
like
I have bunch of routes
Route::group(['prefix' => 'agent'], function(){
Route::get('pay', 'PaymentController#pay');
Route::get('pay/success', 'PaymentController#success');
Route::get('pay/failure', 'PaymentController#failure');
Route::get('credits', 'PaymentController#credits');
Route::get('checkout', 'PaymentController#checkout');
});
As you can see they all are using same route so is there any way to make this as dry as possible i know those are small route but when it goes long line then it become hard to understand i know it's kind of stupid question
is there any attribute like
Route::group(['prefix' => 'agent', 'controller' => 'PaymentController'], function(){
Route::get('pay', 'pay');
Route::get('pay/success', 'success');
Route::get('pay/failure', 'failure');
Route::get('credits', 'credits');
Route::get('checkout', 'checkout');
});
No there isn't any option to define default controller for a route group. But if you have resource routes then it defines all the sub routes by itself, though it's limited to only CRUD routes. You can do something like this if you're interested.
Route::group(['prefix' => 'agent'], function($controller = 'TestController#') {
Route::get('pay', $controller.'pay');
Route::get('pay/success', $controller.'success');
Route::get('pay/failure', $controller.'failure');
Route::get('credits', $controller.'credits');
Route::get('checkout', $controller.'checkout');
});

laravel 5.1 routes groups takes only the first controller?

Route::group(['prefix' => 'api'], function () {
Route::controller(null, 'BoxController');
Route::controller(null, 'CostController');
});
This is a routed group in Laravel 5.1 the urls for the first controller is working but not for the second one 'CostController'.
If I switch the lines the first one works only. I want both controllers url to be prefixed with ...api/box/ and ...api/cost/
Examples on the internet has only one controller in the group, maybe there is another syntax?
I want the urls like : ( because I work on REST application)
api/cost
api/box
not like:
api/cost/cost
api/box/box
Why are you using null for route?
If you'll use different routes, both will work:
Route::group(['prefix' => 'api'], function () {
Route::controller('box', 'BoxController');
Route::controller('cost', 'CostController');
});
Passing null as route is one thing but the main culprit is that you are passing the same route for different controllers. The solution is to use real routes for the controllers, so. i.e. box for BoxController and cost for the other. It will work correctly then
Route::group(['prefix' => 'api'], function () {
Route::controller('box', 'BoxController');
Route::controller('cost', 'CostController');
});

Going to next matching route if no model was found

Let's assume we have 2 such routes:
Route::get('{categoryitem}', ['as' => 'category_index', 'uses' => 'CategoryDisplayController#index']);
Route::get('{entryitem}', ['as' => 'entry', 'uses' => 'EntryController#show']);
There are no parameter constraints for parameters and 2 following route model bindings are defined:
Route::bind('categoryitem', function($slug)
{
return Category::whereSlug($slug)->root()->firstOrFail();
});
Route::bind('entryitem', function($slug)
{
return Entry::whereSlug($slug)->firstOrFail();
});
Now let's assume that URL we run is http://project/something. Is it possible to make Laravel look first in route categoryitem for something slug and in case no model is found it will look in the second route for entry with slug something? I haven't found solution for this other than adding some prefix/suffix to route
"Skipping" routes on a condition is not possible at the moment. Although there is a request on github that would allow skipping on a condition if implemented by the framework...
In the meantime you will have to write one route that catches them all. And determine inside of that route (or controller if you wish) if the item exists.
Route::get('{slug}', function($slug){
// check if categoryitem exists
// else check if entryitem exists, etc...
});
Obviously if this becomes a bit two much code move it into a controller

Resources