dsdapobp26141
2015-05-14 16:08
浏览 34
已采纳

路由在Laravel 5.0上无法正常工作(404错误)

I switched to Laravel 5.0 and try to introduce myself.

I have a small problem which confuses me:

I use two Controller classes. The first works fine but the second one gets ignored. Could somebody tell me why?

routes.php

<?php
Route::controller('/', 'IndexController');
Route::controller('/rezeptliste', 'RecipelistController');

IndexController.php

<?php namespace App\Http\Controllers;

class IndexController extends Controller {
    public function getIndex() {
        return view('index');
    }
}

RecipelistController

<?php namespace App\Http\Controllers;

class RecipelistController extends Controller {
    public function getIndex() {
        return view('recipelist');
    }
}

When I access / it works perfectly. When I try to access /rezeptliste I get the following error:

NotFoundHttpException in Controller.php line 259:
Controller method not found.
in Controller.php line 259
at Controller->missingMethod('rezeptliste')
at call_user_func_array(array(object(IndexController), 'missingMethod'), array('_missing' => 'rezeptliste')) in Controller.php line 246
at Controller->callAction('missingMethod', array('_missing' => 'rezeptliste')) in ControllerDispatcher.php line 162
at ControllerDispatcher->call(object(IndexController), object(Route), 'missingMethod') in ControllerDispatcher.php line 107
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 108
at ControllerDispatcher->callWithinStack(object(IndexController), object(Route), object(Request), 'missingMethod') in ControllerDispatcher.php line 67
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\IndexController', 'missingMethod') in Route.php line 204
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134
at Route->run(object(Request)) in Router.php line 701
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Router.php line 703
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 670
at Router->dispatchToRoute(object(Request)) in Router.php line 628
at Router->dispatch(object(Request)) in Kernel.php line 214
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 43
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 115
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douyou3619 2015-05-14 16:20
    已采纳

    The problem here is that in Laravel routes are evaluated in the order listed.

    This means that since your very first route '/' matches the route /rezeptliste too, laravel will instead go to the IndexController and look for a method called "rezeptliste".

    Instead your routes should be ordered like this:

    <?php
    Route::controller('/rezeptliste', 'RecipelistController');
    Route::controller('/', 'IndexController');
    

    That way your specialized url of /rezeptliste will be "caught" first and sent to the RecipelistController, rather then being caught by the IndexController route.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dqd54099 2015-05-14 16:26

    See section Adding Additional Routes To Resource Controllers of the docs

    If it becomes necessary for you to add additional routes to a resource controller beyond the default resource routes, you should define those routes before your call to Route::resource:

    Route::get('photos/popular', 'PhotoController@method');
    Route::resource('photos', 'PhotoController');
    
    评论
    解决 无用
    打赏 举报
  • drdawt9210 2015-05-14 17:14

    if you use Route:controller() then you have to mention request type in your controller method too. like this

    class UserController extends Controller{
        public function getUserinfo(){
            //
        }
    
        public function postNewuser(){
            //
        }
    
    }
    

    so route can easily identify your request method.

    now you can configure you route

    Route::controller('user','UserController');
    

    I think it will work fine

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题