Passing a variable to a Localization route - Laravel 5.2 - php

Using the following package: laravel-localization .
I am translating the routes and followed the steps, they all work fine for routes without variables, but i'm stuck on how i should send my variables inside my views.
Link inside my view :
Edit Link
routes.php files inside Lang/fr & Lang/nl
<?php
return [
'account-edit' => "account/wijzig-gegevens/{id}",
];
<?php
return [
'account-edit' => "donnees/modifier-donnees/{id}",
];
Laravel routes file:
Route::group([
'prefix' => LaravelLocalization::setLocale(),
'middleware' => ['localize','localeSessionRedirect', 'localizationRedirect' ]
], function()
{
Route::get(LaravelLocalization::transRoute('routes.account-edit'),'AccountController#edit');
});
I tried just adding it inside the route as array like below, but i can't get it working.
Edit Link

Not using the library myself, but according to the code at the github repo, method localizeURL takes in $url and $locale as its parameter, which means that passing in 2nd parameter like you did definitely won't work.
Can you try using method getLocalizedURL?
LaravelLocalization::getLocalizedURL(null, trans('routes.account-edit'), ['id' => $user->id])

Related

Sorry the page you're looking could not be found. Laravel 5.5

I have laravel 5.5. Everything is working fine in it. I can access all routes.
But somehow I cannot access the route "/interest/{$id}".
This is my route.
<?php
Route::get('/interest/{$id}', [
'uses' => 'IdeaController#createInterest'
])->name('interest.create')->middleware('auth');
I Can access every route accept this one. I hope this helps.
Remove the dollar sign from your route parameter:
Route::get('/interest/{id}', [
'uses' => 'IdeaController#createInterest'
])->name('interest.create')->middleware('auth');
Route::get('/interest/{id}', [
'uses' => 'IdeaController#createInterest'
])->name('interest.create')->middleware('auth');
//in controller use method
public function createInterest($id){
}

Laravel passing variables inside route accessing a named route

I am trying to pass 3 variables values inside an html link that uses route to access a named routed with this code:
{{$nomos->name}}
also the route function is
Route::get('gegonota/{gid?}/{cid?}/{nid?}', ['uses' => 'GegonosController#index', 'as' => 'gegonota']);
but it doesnt work right.
How can i do it.
thanks
you can try this.
{{$nomos->name}}
Just remove ? from your routes params so it will be :
Route::get('gegonota/{gid}/{cid}/{nid}', [
'uses' => 'GegonosController#index',
'as' => 'gegonota'
]);
if you want to use ? you need to provide a default value check the docs
and if you use Laravel v5.4 you need to change your route to :
Route::get('gegonota/{gid}/{cid}/{nid}', [
'uses' => 'GegonosController#index'
])->name('gegonota');

Laravel url alias, mismatch in url generation

I have a Laravel 5.2 application where I want to display the same page on 2 different domains / routes. I have it working using the following route structure:
The routes to my primary domain:
Route::group(['domain' => 'www.primarydomain.com',
'prefix' => 'demo-page']), function(){
Route::get('/my-page', 'MyController#index');
Route::get('/my-second-page', 'MyController#getPageTwo');
}
The routes to my secundary domain (note: no prefix!):
Route::group(['domain' => 'www.secundarydomain.com',]), function(){
Route::get('/my-page', 'MyController#index');
Route::get('/my-second-page', 'MyController#getPageTwo');
}
The idea is that both routes will work, and they do. Both www.secundarydomain.com/my-page and www.primarydomain.com/demo-page/my-page work.
The issue is when I now want to generate a link to my second page. For building my URL's in my views, I'm using the following function to generate a link to my-second-page:
url('/my-page')
This function always generates a link to www.primarydomain.com/my-page, while I need a link to www.primarydomain.com/demo-page/my-page.
Is there any easy solution to resolve this? Can this be resolved using middleware, or will a custom URL function be needed?
Expected results:
url('my-page') on www.primarydomain.com should generate a link to www.primarydomain.com/demo-page/my-page
url('my-page') on www.secondarydomain.com should generate a link to www.secondarydomain.com/my-page
Easiest way to do that is to create your own helper, like custom_url() and use it instead of url().
You can look how original url() helper works and create similar one. Look here:
https://github.com/laravel/framework/blob/5.3/src/Illuminate/Foundation/helpers.php#L806
You can assign aliases to your routes.
Route::group(['domain' => 'www.primarydomain.com', 'prefix' => 'demo-page']), function(){
Route::get('/my-page', [
'as' => 'my_page_prefixed',
'uses' => 'MyController#index'
]);
Route::get('/my-second-page', [
'as' => 'my_second_page_prefixed'
'uses' => 'MyController#getPageTwo'
]);
}
And then you can call your aliased route on your blade templates by using {{ route('my_page_prefixed') }} or any other alias.

Laravel 5.2 Localization , auth::user value not showing up when switching language

Using Laravel 5.2.41
Using following translation package: mcamara/laravel-localization
Link inside my view :
Edit Link
routes.php files inside Lang/fr & Lang/nl
<?php
return [
'account-edit' => "account/wijzig-gegevens/{id}",
];
<?php
return [
'account-edit' => "donnees/modifier-donnees/{id}",
];
Laravel routes file:
Route::group([
'prefix' => LaravelLocalization::setLocale(),
'middleware' => ['localize','localeSessionRedirect', 'localizationRedirect' ]
], function()
{
Route::get(LaravelLocalization::transRoute('routes.account-edit'),'AccountController#edit');
});
When i look at the link in my default language (nl) i get the correct link like so:
Edit Link
But when i change my language to french i get following link:
Edit Link
Can't figure out why this is happening
I was looking into the code of that package.
It seems to me if I'm not mistaken, that the logic regarding the translation of URL's is based on the route name, not the route path.
You are using the route path here:
LaravelLocalization::getLocalizedURL(null, trans('routes.account-edit'), ["id" => Auth::user()->id])
But it seems like you should actually use the route name instead, meaning 'account-edit' in this case.

Laravel route starting with a specific string

I need to create a route with some fixed and dynamic parts. Basically I need to be flexible on the second segment on the url. If the url starts with 'products/test....' then the route has to go to the PageController, all other routes starting with 'products/....' have to go to the ProductController.
// Something like this:
Route::any('products/".starts_with($slug, 'test'), [
'uses' => 'PageController#show'
])->where('slug', '(.*)?');
Route::get('products/{slug}', [
'uses' => 'ProductController#show'
]);
Is this possible in Laravel 5?
In Laravel 5 we use Middleware as helpers for routes.
There are some examples in the default installation that you can adapt for your code. This is the best approach for this issue.

Categories