Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route collector error when changing locale #11

Closed
desseb opened this issue Oct 5, 2013 · 4 comments
Closed

Route collector error when changing locale #11

desseb opened this issue Oct 5, 2013 · 4 comments

Comments

@desseb
Copy link

desseb commented Oct 5, 2013

I have a fairly simple way of switching locales by adding the language code to the beginning of the route. Then in my global before filter, I just grab segment(1) and deal with locale switching as applicable. I am using Laravel 4 (laravel/framework (4.0.x-dev b78c92d)).

Unfortunately, when the route collector is set to true, I receive the following error:

ErrorException

Argument 2 passed to Barryvdh\Debugbar\DataCollector\SymfonyRouteCollector::getRouteInformation() must be an instance of Symfony\Component\Routing\Route, null given, called in C:\xampp\htdocs\myproject\vendor\barryvdh\laravel-debugbar\src\Barryvdh\Debugbar\DataCollector\SymfonyRouteCollector.php on line 24 and defined

My global before filter:

App::before(function($request)
{
    if ( in_array(Request::segment(1), Config::get('app.languages')) ) {
        Session::put('locale', Request::segment(1));
        return Redirect::to(substr(Request::path(), 3));
    }
    if ( Session::has('locale') ) {
        App::setLocale(Session::get('locale'));
    }
});

I have the latest update as per composer and the package is installed using dev-master.

This is how I'm setting up Debugbar in my project:

use DebugBar\StandardDebugBar;

$debugbar = new StandardDebugBar();
$debugbarRenderer = $debugbar->getJavascriptRenderer();
$pdo = new DebugBar\DataCollector\PDO\TraceablePDO(new PDO('mysql:host=localhost;dbname=db','user',''));
$debugbar->addCollector(new DebugBar\DataCollector\PDO\PDOCollector($pdo));

and in the html:

{{ $debugbarRenderer->renderHead() }}
{{ $debugbarRenderer->render() }}

Please let me know if you require any additional information.

@barryvdh
Copy link
Owner

barryvdh commented Oct 5, 2013

Are you not using the service provider? What version exactly? 4.0.6, 4.0
dev or 4.1?
Op 5 okt. 2013 05:55 schreef "desseb" notifications@github.com:

I have a fairly simple way of switching locales by adding the language
code to the beginning of the route. Then in my global before filter, I just
grab segment(1) and deal with locale switching as applicable. I am using
Laravel 4.

Unfortunately, when the route collector is set to true, I receive the
following error:

ErrorException

Argument 2 passed to Barryvdh\Debugbar\DataCollector\SymfonyRouteCollector::getRouteInformation() must be an instance of Symfony\Component\Routing\Route, null given, called in C:\xampp\htdocs\emsv3\vendor\barryvdh\laravel-debugbar\src\Barryvdh\Debugbar\DataCollector\SymfonyRouteCollector.php on line 24 and defined

My global before filter:

App::before(function($request){
if ( in_array(Request::segment(1), Config::get('app.languages')) ) {
Session::put('locale', Request::segment(1));
return Redirect::to(substr(Request::path(), 3));
}
if ( Session::has('locale') ) {
App::setLocale(Session::get('locale'));
}});

I have the latest update as per composer and the package is installed
using dev-master.

This is how I'm setting up Debugbar in my project:

use DebugBar\StandardDebugBar;
$debugbar = new StandardDebugBar();$debugbarRenderer = $debugbar->getJavascriptRenderer();$pdo = new DebugBar\DataCollector\PDO\TraceablePDO(new PDO('mysql:host=localhost;dbname=emsv3','root',''));$debugbar->addCollector(new DebugBar\DataCollector\PDO\PDOCollector($pdo));

and in the html:

{{ $debugbarRenderer->renderHead() }}
{{ $debugbarRenderer->render() }}

Please let me know if you require any additional information.


Reply to this email directly or view it on GitHubhttps://github.com//issues/11
.

@desseb
Copy link
Author

desseb commented Oct 5, 2013

4.0.7 it looks like.

The service provider and facade are setup in my config/app.php, if that's what you mean. The debugbar is working, except with the scenario above.

Route even works fine as long as I don't browse to myproject.localhost/fr or myproject.localhost/en (even with additional stuff on the URI).

I suspect the issue stems from the fact that I remove the locale code from the route, but I'm too new to Laravel to know for sure.

@barryvdh
Copy link
Owner

barryvdh commented Oct 5, 2013

Okay, that is because there isn't an actual matching route for the locale link. I added a check instead of forcing the type, so it shouldn't produce an error anymore. 2 things though:

  1. I wouldn't use locales like this. It causes errors when sharing links and for SEO (same url, different content). Why not leave the prefix in front of your url and use a route prefix? Something like this:
$languages = Config::get('app.languages');
$locale = Request::segment(1);
if(in_array($locale, $languages)){
    \App::setLocale($locale);
}else{
    $locale = null;
}
Route::group(array('prefix' => $locale), function()
{
    Route::get('/', array('as' => 'home', function(){ //Do something  }));
    Route::get('news', array('as' => 'news', 'uses' => 'NewsController@showIndex'));
    Route::get('{slug}', 'PageController@showPage');
});

And 2': You don't have to setup the debugbar etc ($debugbar = new StandardDebugBar();), it is set up by the service provider. The debugbar is also rendered by a App::after() listener.
If you do need the debugbar instance, use the IoC container (App::make('debugbar')).

@desseb
Copy link
Author

desseb commented Oct 5, 2013

Confirmed this has resolved the issue. Thank you very much for putting together that example, but I'm not really concerned about SEO since this is an intranet app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants