Laravel - routing and language code - laravel-4

I need to put a language code in my url but not when it is the default one.
Here is the code in routes.php file in Laravel 4.2
I need root structure like:
default language => http://website.com/RegistrationStep1
other language => http://website.com/language/RegistrationStep1
Route::group(['prefix' => '{lang?}', 'before' => 'localization'], function()
{
Route::get('/', function() {
return View::make('hello');
});
Route::get('registration/step1', 'RegistrationController#getRegistrationStep1');
Route::post('registration/step1', 'RegistrationController#postRegistrationStep1');
});
I am getting error when I call url without the language param in url

First, define your available languages:
# in app/config/app.php
'available_locales' => array('de', 'en', 'es', 'fr', 'it'),
In your routes.php check if the first segment of current URI is a valid language shortcut before register your prefix in route group.
$locale = Request::segment(1);
if (in_array($locale, Config::get('app.available_locales'))) {
\App::setLocale($locale);
} else {
$locale = null;
}
Route::group(array('prefix' => $locale), function()
{
//your routes here
});
See the link http://learninglaravel.net/language-switching-in-laravel-4/link
You can also use this package for your task: mcamara/laravel-localization

Related

simulate route in laravel

I have this route defined
Route::get('test/something/{id?}', 'MyController#mymethod')->name('test.something');
So if go to domain_1.com/test/something/123 I get some page with data.
Now, I want to to show the exact same thing if the site if accessed form another domain with subdomain. I've defined this:
Route::group(['domain' => 'subdomain.domain_2.com'], function() {
Route::get('/', 'MyController#mymethod', ['id' => 123]);
});
but I got to subdomain.domain_2.com, I get Too few arguments error. How can I pass the id parameter to the method in the route?
You could use redirect like:
Route::get('test/something/{id?}', 'MyController#mymethod')->name('test.something');
Route::group(['domain' => 'subdomain.domain_2.com'], function() {
Route::get('/', function(){
return redirect('/test/something/123');
});
});
If you don't want to use redirect, you could check the current url from the method in the controller like:
public function mymethod($id=null){
$currentUrl = request()->url();
if (strpos($currentUrl, 'subdomain') !== false) {
$id = 123;
}
// your function
}
I've found this piece of code that gets the job done, but I'm not sure is the correct way to achieve what I want
Route::group(['domain' => 'subdomain.domain_2.com'], function()
{
Route::get('/', [ 'as' => 'some_alias', function()
{
return app()->make(App\Http\Controllers\MyController::class)->callAction('mymethod', $parameters = [ 'id' => 123 ]);
}]);
});

Laravel Localization 2 lang website return wrong locales

I'm using Laravel5.5 ,I want to make 2 lang website , when I try to use return App::getLocale();
and I go /en/test It should return en and /sv/test It should return sv but now It only return en
first I set up in my config.app
'locales' => ['en' => 'English', 'sv => 'Sweden'],
After that I setup my middleware language.php
if (Session::has('applocale') AND array_key_exists(Session::get('applocale'), Config::get('languages'))) {
App::setLocale(Session::get('applocale'));
}
else { // This is optional as Laravel will automatically set the fallback language if there is none specified
App::setLocale(Config::get('app.fallback_locale'));
}
return $next($request);
Then In my kernel.php I setup routemiddleware
'language' => \App\Http\Middleware\Language::class,
last thing I set up my web.php
Route::group(['middleware' => ['web','language'] ,'prefix' => 'sv' ], function () {
Route::get('/test', function(){
return App::getLocale();
});
});
Route::group(['middleware' => ['web','language'] ,'prefix' => 'en' ], function () {
Route::get('/test', function(){
return \App::getLocale();
});
});
You so not config language names, just codes (aside from that it's Svenska)
'locales' => ['en', 'sv'];

Laravel group routing with parameter

I have this group in my route:
}
Route::group(['prefix' => 'v1/en'], function() {}
is it possible to change the 'en' segment uri in a parameter, so it can be changed depending on the request? Something like this:
Route::group(['prefix' => 'v1/{lang}'], function() {}
Yes you can, $lang will be available if you define your route group like this:
Route::group(['prefix' => 'v1/{lang}'], function() {
// ...
});
or with new syntax:
Route::prefix('v1/{lang}')->group(function() {
// ...
});
You can access it anywhere with:
request()->route()->parameter('lang');
You might want to checkout packages like this: Localization for Laravel
They basically do the same, and have nice middleware implementations to set locales etc.
How about to generate dynamically another route group with prefix $lang inside of group with v1 prefix?
$langs = ['en', 'de', 'it'];
Route::group(['prefix' => 'v1'], function() use ($langs) {
foreach($langs AS $lang) :
Route::group(['prefix' => $lang], function() {
Route::get('something', 'SomethingController#list');
});
endforeach;
});
or same logic (taken from here):
Route::group(['prefix' => 'v1'], function() use ($langs) {
Route::group(['prefix' => '{lang}'], function() {
Route::get('something', 'SomethingController#list');
});
});

Multi domain routing in Laravel 5.2

I have setup multi-domain routing in my laravel 5.2 app. What I want to achieve is if a user hits, membership.app, he should be served different homepage as compared to user who hits, erp.app domain.
Route::pattern('erp', 'erp.app|erp.domain.com');
Route::pattern('membership', 'membership.app|membership.domain.com');
Route::group(['middleware' => ['web', 'auth'], 'domain' => '{erp}'], function() {
Route::get('/', 'HomeController#getIndex');
Route::controller('members', 'MembersController');
Route::controller('users', 'UsersController');
Route::controller('settings', 'SettingsController');
});
Route::group(['middleware' => 'web', 'domain' => '{erp}'], function () {
Route::controller('auth', 'Auth\AuthController');
});
Route::group(['middleware' => 'web', 'domain' => '{membership}'], function () {
Route::controller('/', 'BecomeMemberController');
});
Route::group(['middleware' => 'web'], function () {
Route::controller('ajax', 'AjaxController');
});
I tried this setup, but it breaks the code with first param in each controller method being the url instead of intended value.
Suppose I have a method hello in members controller.
public function hello($param1, $param2)
{
....
}
If I access erp.app/members/hello/1/2 url and try to print out $param1 of controller method, it returns erp.app instead of intended 1 in this case.
Please help.
I don't know why aren't you seperating the routes to different controllers as you say the output will be quite different...
A quick example of to use that:
Route::group(['domain' => '{type}.myapp.com'], function () {
Route::get('members/hello/{id1}/{id2}', function ($type, $id1, $id2) {
// when you enter --> members.myapp.com/hello/12/45
var_dump($type); //memebers
var_dump($id1); //12
var_dump($id2); //45
});
});

Laravel 4 : Route returning wrong information

I am setting up an API with Laravel so that I can connect with an AngularJS front-end.
I have 2 routes that go to the same controller method -> BallController#getSpecs
My routes are set up as follows:
Route::group(['prefix' => '/api/v1', 'before' => 'auth.basic'], function() {
Route::group(['prefix' => '/ball'], function() {
Route::get('/', 'BallController#getIndex');
Route::get('{id}', 'BallController#getIndex');
Route::get('specs', 'BallController#getSpecs');
Route::get('{id}/specs', 'BallController#getSpecs');
});
});
Now I am having trouble with Route::get('specs', 'BallController#getSpecs'); route.
The getSpecs method is defined as follows:
public function getSpecs($id = null)
{
if(empty($id))
{
Ball::all()->each(function($ball) {
$json = [$ball->id => [
'name' => $ball->name,
'core' => $ball->core->toArray(),
'coverstock' => $ball->coverstock->toArray(),
'finish' => $ball->finish->toArray(),
'manufacturer' => $ball->manufacturer->toArray()
]];
});
return Response::json($json);
}
else
{
$ball = Ball::find($id);
if(empty($ball))
{
return Response::json('You\'ve got no ballss', 404);
}
else
{
return Response::json([$ball->id => [
'name' => $ball->name,
'core' => $ball->core->toArray(),
'coverstock' => $ball->coverstock->toArray(),
'finish' => $ball->finish->toArray(),
'manufacturer' => $ball->manufacturer->toArray()
]]);
}
}
}
When I call /api/v1/ball/1/specs specifying an id I get the correct information back, however when I call /api/v1/ball/specs my function returns my error message 'You've got no balls'
ID should be null in this cast putting me into the first part of my if statement but for some reason I am getting into my else and getting my error because obviously no ID was provided and the ball won't exist.
Any help/insight will be appreciated.
Edit: I think it may be sending it to the wrong method. I think that /api/v1/ball/specs is being sent to BallController#getIndex instead of BallController#getSpecs.
The problem is in this part of the routing:
Route::get('{id}', 'BallController#getIndex');
Route::get('specs', 'BallController#getSpecs');
When it sees "/api/v1/ball/specs", it will actually call getIndex() with the "id" parameter set to "specs", because that's the first matching route. One way to fix it would be to define the "specs" route first before you define the "{id}" route. But a better solution would be to limit the accepted values of the "{id}" parmeter like this...
Route::get('{id}', 'BallController#getIndex')->where('id', '[0-9]+');
One other suggestion, there shouldn't be a leading "/" in your prefix values (apparently the code may be working anyway, but they aren't really supposed to be there. So the complete updated route might look like this...
Route::group(['prefix' => 'api/v1', 'before' => 'auth.basic'], function() {
Route::group(['prefix' => 'ball'], function() {
Route::get('/', 'BallController#getIndex');
Route::get('specs', 'BallController#getSpecs');
Route::get('{id}', 'BallController#getIndex')->where('id', '[0-9]+');
Route::get('{id}/specs', 'BallController#getSpecs')->where('id', '[0-9]+');
});
});

Resources