10

Ok, the full routes.php file that I use... I just pasted it here: http://pastebin.com/kaCP3NwK

routes.php

//The route group for all other requests needs to validate admin, model, and add assets
Route::group(array('before' => 'validate_admin|validate_model'), function()
{
    //Model Index
    Route::get('admin/(:any)', array(
        'as' => 'admin_index',
        'uses' => 'admin@index'
    ));

administrator config:

...
'models' => array(
'news' => array(
    'title' => 'News',
    'single' => 'news',
    'model' => 'AdminModels\\News',
),
...

links generator:

@foreach (Config::get('administrator.models') as $key => $model)
    @if (Admin\Libraries\ModelHelper::checkPermission($key))
        <?php $key = is_numeric($key) ? $model : $key; ?>
        <li>
            {{ HTML::link(URL::to_route('admin_index', array($key)), $model['title']) }}
        </li>
    @endif
@endforeach

controllers/admin.php

public function action_index($modelName)
{
    //first we get the data model
    $model = ModelHelper::getModelInstance($modelName);

    $view = View::make("admin.index",
        array(
            "modelName" => $modelName,
        )
    );

    //set the layout content and title
    $this->layout->modelName = $modelName;
    $this->layout->content = $view;
}

So, when accessing http://example.com/admin/news the news is sent to action_index... but for some reason it doesn't get there and it returns 404

tereško
  • 58,060
  • 25
  • 98
  • 150
Alex
  • 7,538
  • 23
  • 84
  • 152
  • Is your bundle registered to handle routes? Does [http://mysite.com/admin](http://mysite.com/admin) load your index page? http://laravel.com/docs/routing#bundle-routes – radicalpi Jan 04 '13 at 16:00
  • @ChrisHendry Yes, everything works by default... and I already added a new router to bundle's file, a route for uploading images and it works great – Alex Jan 04 '13 at 16:07
  • does it still 404 if you debug with an anonymous function and return in the route? (without trying to point to the controller/method) – aowie1 Jan 08 '13 at 16:19
  • If Laravel is unable to find a route, it returns a prettier error than the server. Are you getting the Laravel 404 or the server's 404? – J.T. Grimes Jan 08 '13 at 17:08
  • I'm getting Laravels's 404 error... if it were to be more of a apache url rewriting issue, that would've been easier to resolve... I'm sure about that :D – Alex Jan 08 '13 at 17:14

3 Answers3

6

Notice that I defined the following 'model' => 'AdminModels\\News',

when actually my namespace register was Admin\Models, so setting it to 'model' => 'Admin\Models\\News', for my issue for the 404

Alex
  • 7,538
  • 23
  • 84
  • 152
3

Routes are evaluated in the order that they're registered, so the (:any) route should be last. You're being sent (I think) to admin@index -- if that function isn't defined yet, that's why you're getting a 404.

J.T. Grimes
  • 4,222
  • 1
  • 28
  • 32
  • There must not be an "action_index" in w0rldart's administrator bundle's admin controller. That would explain the 404 – Joel Larson Jan 06 '13 at 10:06
  • Actually, `action_index` is defined. You can view it here https://github.com/FrozenNode/Laravel-Administrator/blob/master/controllers/admin.php – Alex Jan 08 '13 at 10:11
2

Not related to the question, but if anyone (like me) comes here to find clues why a Laravel application displays a 404, here are some reasons:

  • Models not found in database that are specified in the URL
  • You have setup incorrect route model bindings in RouteServiceProvider (as I did when I stumbled over this answer). Example: Route::model('user', Tenant::class); when it should be User::class
  • Some middleware returns 404 (e.g. via "abort(404)")
  • The corresponding controller returns 404
  • The controller method (or namespace) is not found (This is the answer for this question.)