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

Deep namespaced routes not working #8404

Closed
Loschcode opened this issue Apr 12, 2015 · 10 comments
Closed

Deep namespaced routes not working #8404

Loschcode opened this issue Apr 12, 2015 · 10 comments

Comments

@Loschcode
Copy link

I'm building an international website, therefore I managed to have URLs looking like /{language}/{other_stuff} thank to some manipulation in RouteServiceProvider

/**
 * Define the routes for the application.
 *
 * @param  \Illuminate\Routing\Router  $router
 * @return void
 */
public function map(Router $router, Request $request)
{

    $locale = $request->segment(1);
    $this->app->setLocale($locale);

    /**
     * Internationalization routing system
     */
    $router->group(['namespace' => $this->namespace, 'prefix' => $locale], function($router) use ($locale) {

        if ($locale == 'en') require app_path('Http/routes_en.php');
        elseif ($locale == 'el') require app_path('Http/routes_el.php');

    });

}

Works like a charm. Every language will have his own route file, it's a choice.

Let's say we go to /en/ and you're an admin, I created another namespace within Http/route_en.php to focus on the admin section :

Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function() {

  Route::controller('', 'DashboardController');

  Route::controller('brands', 'BrandsController');
  Route::controller('contents', 'ContentsController');
  Route::controller('downloads', 'DownloadsController');
  Route::controller('news', 'NewsController');
  Route::controller('products', 'ProductsController');
  Route::controller('settings', 'SettingsController');
  Route::controller('users', 'UsersController');

});

So now I should access easily sections such as /en/admin/brands but it fails. I generate all my links dynamically thanks to the HTML class

{!! HTML::linkAction('Admin\BrandsController@getIndex', 'Brands') !!}

The generation works fine when I go to /en/admin which means Admin\BrandsController@getIndex is detected by this package, but when you click on it

Sorry, the page you are looking for could not be found.

I tested some stuff and when I just simply set the route outside group() it works fine.

Route::controller('admin/brands', 'Admin\BrandsController');

What am I missing here ? Shouldn't the HTML class and the routing system agree with each others ? Is there any mistake I made ? Maybe there's an issue ?

@rkgrep
Copy link

rkgrep commented Apr 13, 2015

try to call artisan route:list.
and according to your code, /en/admin should lead to Admin\DashboardController@getIndex - is the method present in controller?

@Loschcode
Copy link
Author

It is, and when I call all those routes outside the namespace it works fine. Also, I noticed that the DashboardController@getIndex was working even in the namespace.

website(master)$ php artisan route:list
Your application doesn't have any routes.

Well this is very very weird because my application is already full of routes ... Most of them working

@rkgrep
Copy link

rkgrep commented Apr 13, 2015

OK. artisan isn't working because routes are included only of request segment is present (It is not in CLI mode) - I didn't notice it.

Try to debug routes at the end of your routes_en.php

dd($router->getRoutes()->getRoutes()); // Line correct - call getRoutes twice

@Loschcode
Copy link
Author

Very interesting debugging functionality !

One of the routes looks like that

  12 => Route {#148 ▼
      #uri: "en/admin/brands"
      #methods: array:2 [▼
        0 => "GET"
        1 => "HEAD"
      ]
      #action: array:7 [▼
        "middleware" => "anything"
        "uses" => "App\Http\Controllers\Admin\BrandsController@getIndex"
        "as" => null
        "controller" => "App\Http\Controllers\Admin\BrandsController@getIndex"
        "namespace" => "App\Http\Controllers\Admin"
        "prefix" => "en/admin"
        "where" => []
      ]
      #defaults: []
      #wheres: []
      #parameters: null
      #parameterNames: null
      #compiled: null
      #container: Application {#2}
    }

And going to /en/admin/brands leads to Sorry, the page you are looking for could not be found.

What do you think ?

PS : i built a anything middleware but it return true every time right now so it doesn't really matter, just for you to understand the full route details

@Loschcode
Copy link
Author

Here's the full route_en.php file

Route::group(['middleware' => 'anything'], function() use ($router) {

      Route::controller('admin/news', 'Admin\NewsController');

      /**
       * Admin
       */
      Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function() use ($router) {

        Route::controller('', 'DashboardController');

        Route::controller('brands', 'BrandsController');
        Route::controller('contents', 'ContentsController');
        Route::controller('downloads', 'DownloadsController');
        Route::controller('news', 'NewsController');
        Route::controller('products', 'ProductsController');
        Route::controller('settings', 'SettingsController');
        Route::controller('users', 'UsersController');

      });

      /**
       * Auth
       */
      Route::group(['namespace' => 'Auth'], function() use ($router) {

        Route::controller('auth', 'AuthController');
        Route::controller('password', 'PasswordController');

      });

      /**
       * Other
       */
      Route::controller('', 'HomeController');

    });

@JosephSilber
Copy link
Member

I'm not entirely sure about this, but I don't think routes are ever supposed to be registered conditionally.

@rkgrep
Copy link

rkgrep commented Apr 14, 2015

I've copied your codes into a fresh laravel installation and everything works.

Does your middleware return true or $next($request);?

@Loschcode
Copy link
Author

So it's getting weird. My middleware does exactly this

<?php namespace App\Http\Middleware;

    use Closure;
    use \Illuminate\Routing\Route as Route;

    class Anything {

      /**
       * Create a new filter instance.
       */
      public function __construct()
      {

      }

      /**
       * Handle an incoming request.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  \Closure  $next
       * @return mixed
       */
      public function handle($request, Closure $next)
      {

        // Every time it will be true
        return $next($request);

      }

    }

I return $next, not true, my bad.

So I removed it and it doesn't change anything to my routing problem. I'm starting to be worried not finding any solution :(

Also, even if the routes aren't supposed to be registered conditionally, it's just a require, i don't think it may change anything when it deals with the system

@rkgrep
Copy link

rkgrep commented Apr 16, 2015

Your code definetly works - I've checked.
So the problem is not in routing.

Try to make a new fresh installation and copy your code step-by-step.
This is the only solution I can advice.

@shehi
Copy link
Contributor

shehi commented Apr 16, 2015

If php artisan route:list doesn't work, php artisan route:cache might not work either - just a reminder in case if you are relying on faster Route caching.

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

5 participants