php - 使用获取 NotFoundHttpException 的单个 RESTful 路由清理 Laravel 5.1 安装

标签 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 serve 现在会抛出此错误:

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 欢迎 View 。我还尝试在没有组的情况下仅使用上面的内部路由表并点击 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

最佳答案

如果它与 artisan serve 而不是 homestead 一起工作,那么您的 homestead 主机设置可能会将您的请求发送到 homestead 上的默认项目,而不是您设置的项目。请确认您的路线文件顶部的 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 接受最新的更改。

我认为这应该可以解决您的问题。

关于php - 使用获取 NotFoundHttpException 的单个 RESTful 路由清理 Laravel 5.1 安装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30851877/

相关文章:

php - 除了通过引用传递来做我想做的事之外,还有另一种方法吗

PHP Gmail API - 发送草稿

Laravel Vue VueRouter 历史模式

php - Laravel Elixir 没有结合脚本

php - 在 Laravel 8 中创建带有门面的自定义包

php - 您将如何使用 CSS 设计 PHP 后缀的样式?

javascript - 提交大文件 Laravel 和 Ajax

php - 使用 Laravel Voyager 管理包实现 REST API

htmlspecialchars() 期望参数 1 是给定的字符串数组

php - Laravel 5.4 - 404 页面问题