使用单个RESTful路由清除Laravel 5.1安装获取NotFoundHttpException

时间:2015-06-15 17:57:58

标签: php laravel laravel-5

我使用Laravel 5作为Ionic应用程序的API,它似乎几乎正常工作,但我在Laravel的测试路线

Route::group( [ 'prefix' => 'api' ], function ()
{
    Route::any( 'user', function ()
    {
        return 'Hello Anonymous User';
    } );
} );

当我点击

http://localhost:8000/api/user

应该只返回" Hello Anonymous User",但由于切换到Homestead而不是使用artisan服务它现在会抛出此错误:

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

NotFoundHttpException in RouteCollection.php line 145:
in RouteCollection.php line 145
at RouteCollection->match(object(Request)) in Router.php line 719
at Router->findRoute(object(Request)) in Router.php line 642
at Router->dispatchToRoute(object(Request)) in Router.php line 618
at Router->dispatch(object(Request)) in Kernel.php line 210
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 111
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53

从Ionic使用ngResource到API的任何帖子都会在控制台中以500错误的形式返回。我发现一个解决方案建议通过注释掉关闭app / http / Kernel.php中的VerifyCsrfToken中间件:

\App\Http\Middleware\VerifyCsrfToken::class,

但这并没有奏效或改变错误。

我可以在http://localhost:8000/点击默认的欢迎路线,并使用调试栏查看Laravel 5欢迎视图。我也尝试在没有组的情况下使用上面的内部路径表单并点击http://localhost:8000/user,但它会产生相同的错误。

更新

检查路线列表所有路线似乎都没问题。

php artisan route:list

| GET|HEAD                       | /                 | Closure
| GET|HEAD|POST|PUT|PATCH|DELETE | api/user          | Closure
| GET|HEAD|POST|PUT|PATCH|DELETE | user              | Closure

... with debugbar routes

1 个答案:

答案 0 :(得分:1)

如果它与artisan服务而不是宅基地一起工作那么你的宅基地主机设置很可能是将你的请求发送到宅基地的默认项目,而不是你设置的那个。请确认路线文件顶部的dd(“ping”)显示在屏幕上。否则,你可能会看错了应用程序。

检查你机器上的/ etc / hosts文件(假设OS X或* nix),并确保你设置192.168.10.10指向someappname.app并在〜/ .homestead / Homestead.yaml中确保在网站:您添加的部分

-map: someappname.app
 to: /home/whatever the mapped path to this app is

在〜/ Homestead目录中,输入vagrant provision以确保vagrant接受最新更改。

我认为这可以解决你的问题。