Laravel route acts craizy - laravel

Hy i have this http://laravel.io/bin/jaPB
The problem is when i go to:
domain.com -> it serves the homepage (wich is ok)
domain.com/foo -> it serves a subpage (still ok)
but when i go from one of those to:
domain.com/en -> it gives an error (not ok)
But after hitting refresh its ok.
So again when i'm on domain.com/en first time error after refresh ok
Same goes to subpage like domain.com/en/contact first time error after refresh ok
I would point out that the error says first time it tries to go to PublicPageController#subpage
but this shouldn't happen when i go to domain.com/en it should need to go to PublicPageController#homepage
Any idea ?
Thank you all.

My guess here form looking at the way you set up the locale-based routes is that the Session::get('urilang) isn't set the first time you visit, hence the error, and is only set once you've been to a page first.
Now, I haven't yet had to deal with multilingual sites, but as far as I'm aware the way you're doing it is not the correct way. Instead think of the lang key as a URI parameter, and use the filter to validate and set it for the routes. Something a bit like the below code:
// Main and subpage - not default language
Route::group(array('prefix' => '{lang}', 'before' => 'detectLanguage'), function () {
Route::get('', 'PublicPage#homepage');
Route::get('{slug}', 'PublicPage#subpage');
});
// Main and subpage - default language
Route::group(array('before' => 'setDefaultLanguage'), function () {
Route::get('/', 'PublicPage#homepage');
Route::get('/{slug}', 'PublicPage#subpage');
});
Route::filter('detectLanguage', function($route, $request, $response, $value){
// hopefully we could do something here with our named route parameter "lang" - not really on sure the details though
// set default
$locale = 'hu';
$lang = '';
// The 'en' -> would come from db and if there is more i would of corse use in array
if (Request::segment(1) == 'en')
{
$lang = 'en';
$locale = 'en';
}
App::setLocale($locale);
Session::put('uriLang', $lang);
Session::put('locale', $locale);
});
Route::filter('setDefaultLanguage', function($route, $request, $response, $value){
App::setLocale('hu');
Session::put('uriLang', '');
Session::put('locale', 'hu');
});
I don't know if you can use a segment variable in a Route::group prefix, but you should certainly have a go at it as it'd be the most useful.
That said, I wouldn't advise setting up default language routes that mimic specific language routes but without the language segment. If I were you, I'd set up a special root route that will redirect to /{defaultlang}/ just so you have fewer routing issues.

Related

Avoiding Multiple Routes for Different Languages

I'm translating my site into different languages. By the default, the language will be English, which I'm using the following route to return the "welcome" view:
Route::get('welcome', function ()
{
return view('welcome');
});
For my other languages, I'm using this other route:
Route::get('welcome/{locale}', function ($locale)
{
App::setLocale($locale);
return view('welcome');
});
Is there any way I can combine those two routes into one? For example, if the route is "welcome" or "welcome/en", return the "welcome" view in English, the default language.
However, if the route is "welcome/fr", the "welcome" view should be returned in French.
I'm going to have hundreds of routes so I would love being able to combine my routes.
The default language for your application is stored in the config/app.php configuration file. You may modify this value to suit the needs of your application. You may also change the active language at runtime using the setLocale method on the App facade:
Route::get('welcome/{locale}', function ($locale) {
if (! in_array($locale, ['en', 'es', 'fr'])) {
abort(400);
}
App::setLocale($locale);
//
});
You may configure a "fallback language", which will be used when the active language does not contain a given translation string. Like the default language, the fallback language is also configured in the config/app.php configuration file:
'fallback_locale' => 'en',
Occasionally you may need to specify a route parameter, but make the presence of that route parameter optional. You may do so by placing a ? mark after the parameter name. Make sure to give the route's corresponding variable a default value:
Route::get('welcome/{locale?}', function ($locale = null) {
//
});

Laravel 5: Sessions not working the way they should

On top of every controller and routes.php I used:
use Illuminate\Support\Facades\Session;
In routes.php I set the session using:
Session::put('key', 'value');
In a controller I want to call the session value of key using:
echo Session::get('key');
But once I set a new value to key in routes.php and call it in a controller, I still get the first value and not the new one. If I echo the the session using Session::all() in routes.php after setting it, I see the new value, but in a controller it flips back to the first value. I even tried using below in routes.php before setting the new value, but without success.
Session::forget('key');
Am I forgetting something here?
Using regular PHP $_SESSION my routes.php looks like this:
$slug = $_SERVER['REQUEST_URI'];
$slug = explode('/', $slug[0]);
if(in_array($slug[1], Language::all()->lists('iso'))) {
$_SESSION['language'] = $slug[1];
if(!$slug[2]) {
$_SESSION['slug'] = 'home';
Route::any('/{slug}', ['as' => 'pages.page', 'uses' => 'PagesController#page']);
} else {
if($slug[2] != 'dashboard' && $slug[2] != 'migrate' && $slug[2] != 'form-send') {
if (in_array($slug[2], ElementValue::where('element_field_id', 2)->lists('value_char')) && !isset($slug[3])) {
$_SESSION['slug'] = $slug[2];
Route::any('/{slug}', ['as' => 'pages.page', 'uses' => 'PagesController#page']);
} else {
$_SESSION['slug'] = 'home';
Route::any('/{slug}', ['as' => 'pages.page', 'uses' => 'PagesController#page']);
}
}
}
}
Where in routes.php are you setting the session value? It sounds like you're doing something like this:
Session::put('key', 'value');
Route::get('my-route', 'MyController#doSomething');
and then doing this:
class MyController {
public function doSomething()
{
Session::get('key');
}
}
Is that correct? If so, read on...
I'm no expert on the Laravel request lifecycle (for more, see the documentation), but it doesn't surprise me that this doesn't work. The way I think about it is this: the routes.php file is loaded and executed early in the life cycle - probably first - since it tells the application what code to execute next (ie. what do when a particular request is received). And when I say "early in the life cycle", I mean early - like before sessions are initialized. I believe that the Session::put call is simply being ignored, since at the time when you're setting the value, the session does not exist.
You may want expand your question with a little more detail about what you're trying to accomplish - there has got to be a better way to do it.
EDIT - in response to the comments below...
I am not saying you should touch the $_SESSION superglobal - that's a bad idea because I'm not even sure that Laravel uses the native PHP session facility and you have no guarantee that whatever you do will continue to work in the future.
It's not clear what you're trying to do, but to me this sounds like a value that does not belong in the session.
By placing the Session::put in the routes.php file, it sounds like you have some value that's important and should be set for every session and every request
If that's the case, and it's a static value, then it's not a session value, it's a configuration value.
If, instead, it's a dynamic value and/or it changes depending on which user is associated with a session, then you can set it in one of several places:
if you're using controller-based routing, you could set this in the controller constructor, although I wouldn't recommend it, because you will probably have to do it for several controllers, leading to code duplication
if you're using closures in your routes, set it there. E.g.
Route::get('some/route', function () {
Session::put('key', 'value');
// this works, because the closure isn't executed until after
// the application is initialized
});
you could also do it in middleware
or in a service provider (although I'm not certain that sessions would be available when the service providers are executed).
The best option is probably middleware - this would allow you to set (or calculate) the session value in one place in your code and also associate it with particular routes, if you don't need it for all routes.
Don't use $_SESSION in laravel. Uses the laravel Session class. See the following post How to access the globals $_SESSION and $_COOKIE from a laravel app?
Also, all your if logic should not be living in routes.php. You should add that to middleware to filter your routes.
Also, you are really making this hard for yourself. Laravel provides most of what you need in convenient helper classes e.g. Request::url(), Request::getHost(), Request::getLocale(). Have a read through the docs and get familiar with "The Laravel Way" it will be much easier and things will then work as you expect.
I moved the logic to the controller and now my routes are this simple:
Route::pattern('slug', '[a-zA-Z0-9\-_\/]+');
$slug = Request::path();
if(isset($slug)) {
Route::any('/{slug}', 'PagesController#index')->where('slug', '[a-zA-Z0-9\-_\/]+');
}
The session is stored in the PagesController and used further in the application. Thanks for your help guys.

Share variable based on route group

I have 2 versions of a site. One is located in the root URL of the site and one is using a route prefix. They use the same resources but provide different links when accessed from the prefixed route:
Route::get('/', function(){
View::share('outgoing_url','something.com');
//regular links here
});
and a few more of the above pointing to different routes or
Route::group(array('prefix'=>'tour'), function(){
View::share('outgoing_url','somethingelse.com');
//different links here
});
View::share doesn't work since it uses whatever is assigned last so I am trying to find a solution for this problem.
Also, when I use HTML::link() in the views that go through the prefix, everything still points to the root URI of the site instead of the 'tour' prefix. Is there any way to differentiate between the two? Right now I am stuck with this problem and the only solution seems to be to make identical copies of the views and controllers responding to the routes. But that approach seems stupid to say the least.
I hope I explained the problem understandably.
HTML Macro:
<?php
HTML::macro('myLink', function($url, $title = null, $attributes = array(), $secure = null)
{
if (Request::segment(1) === 'tour')
{
$url = 'tour/'.$url;
}
return HTML::link($url, $title, $attributes, $secure);
});
?>
Usage:
HTML::myLink(...);
Just use a before filter - and set it that way
App::before(function($request)
{
if (Request::segment(1) === 'tour')
{
View::share('outgoing_url','tour.com');
}
else
{
View::share('outgoing_url','other.com');
}
});

changing url, and locale on the fly with laravel

My code in laravel to handle multiple language is:
$languages = array('it-IT','en-GB','fr-FR');
$lingua = Request::segment(1);
if(in_array($lingua, $languages)){
\App::setLocale($lingua);
}else{
$lingua = 'it-IT';
}
Route::group(array('prefix' => $lingua), function()
{
Route::get('/', array('as' => 'home', 'uses' => 'ItemController#menu'));
Route::get('/{idcampo}','ItemController#show');
});
How can i:
1)Make the page start always with it-IT as default. (i need it because I use $lingua to fetch from a database) so i can't have that null. Should I use a redirect::to / to /it-IT?
2) change url and language(app:locale) on he fly with a link in the upper section of every pages. withouth returning to the home.
3) to link pages I learn to use:
URL::route('home')
but how to do it when the link change with the entry of a database (for example my link is {{ URL::to($lingua. '/'. $campo[1].'/') }}) I need to use
URL::action('ItemController#show', ($lingua. '/'. $campo[1].'/'))
EDIT:
OK at the top of my pages there is a link to change language on the fly.
Italian //
English //
French
I create a controller clled LanguageController
<?php
class LanguageController extends BaseController {
public function select($lingua)
{
// Store the current language in the session
Session::put('lingua', $lingua);
return Redirect::back(); // redirect to the same page, nothing changes, just the language
}
}
I create a route:
Route::get('lingua/{lingua}', 'LanguageController#select');
Route::get('/', array('as' => 'home', 'uses' => 'ItemController#menu'));
Route::get('/mondo/','ItemController#mondo');
Route::get('/{idcampo}','ItemController#show');
I have my ItemController#menu
public function menu()
{ $linguadefault='it-IT';
$lingua = Session::get('lingua',$linguadefault);
$data = DB::table('campo')->lists('id');
return View::make('index')->with('campo',$data)->with('lingua',$lingua);
}
1) I don't understand why i need to route at lingua/{lingua} if i never route there but i use a url:action to a controller directly.
2) now i need to add
$linguadefault='it-IT';
$lingua = Session::get('lingua',$linguadefault);
at the beginning of every function to have a lingua variable in my page right?
3) now my language seems stucked to french and i can't change it anymore.
I would not use the language in the URL all the time, you can just switch languages when you need and persist it:
1) Use Session to persist the language chosen:
// Set the default language to the current user language
// If user is not logged, defaults to Italian
$linguaDefault = Auth::check()
? Auth::user()->lingua
: 'it-IT';
/// If not stored in Session, current language will be the default one
\App::setLocale(Session::get('lingua', $linguaDefault));
To have the language always set in your application, you can put this code in your file
app/start/global.php
And you don't need to add this anywhere else. So it will use it in this order:
a) Language stored in Session (selected online)
b) Language user has in database
c) Italian
2) To change the language you create a route:
Route::get('lingua/{lang}', 'LanguageController#select');
Your links
URL::action('LanguageController#select', 'it-IT')
URL::action('LanguageController#select', 'en-GB')
URL::action('LanguageController#select', 'fr-FR');
And in your controller you just have to do:
public function select($lang)
{
// Store the current language in the session
Session::put('lingua', $lang);
return Redirect::back(); // redirect to the same page, nothing changes, just the language
}
3) This way you don't need your language in all your URLs, you don't have to deal with it in all your routes. If your user changes the language in database, you just:
$user->save();
Session::put('lingua', $user->lingua);
return Redirect::route('home'); // or anything else

Is there a way to set a default url parameter for the Route class in Laravel 4?

I'm trying to set up sub-domain based routing in Laravel 4 and I've hit a bit of an annoyance...
My route group looks like this:
Route::group(array('domain' => '{company}.domain.com'), function() {
// ...
});
Which seems to work fine, however, I need to specify the company parameter for every route/url I generate. I.e:
{{ HTML::linkRoute('logout', 'Logout', ['company' => Input::get('company')]) }}
Is there any way to specify the company parameter as static/global, so it is automatically added to any links I specify, unless otherwise overwritten/removed?
Unfortunately, no (I haven't seen any evidence in the router or HTMLBuilder that you can). You could, however, make an HTML macro... Example:
HTML::macro('lr', function($link, $title) {
$company = !empty(Input::get('company')) ? Input::get('company') : "";
return HTML::linkRoute($link, $title, ['company' => $company]);
});
Then call it - instead of HTML::linkRoute, use HTML::lr('logout', 'Logout')
Just an idea.

Resources