Clean Laravel 5.1 Install with a single RESTful ro

2019-03-31 03:12发布

问题:

I'm using Laravel 5 as an API for an Ionic app, and it appears to be almost working, but my test route in Laravel

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

When I hit

http://localhost:8000/api/user

Should just return "Hello Anonymous User", but since switching to Homestead instead of using artisan serve it now throws this error:

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

And any post from Ionic using ngResource to the API is returned as a 500 Error in the console. I found one solution that suggested turning off VerifyCsrfToken middleware in app/http/Kernel.php by commenting out:

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

but this didn't work or change the error.

I am able to hit the default welcome route at http://localhost:8000/ and see the Laravel 5 welcome view with debugbar. I also tried using just the inner route form above without the group and hit http://localhost:8000/user, but it produces the same error.

UPDATE

Checking the route list all the routes appear to be okay.

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:

If it works with artisan serve instead of homestead then it is likely that your homestead hosts setup is sending your request to the default project on homestead instead of the one that you have setup. Please confirm that dd("ping") at the top of your routes file shows up on screen. Otherwise, you are likely looking at the wrong app.

Check your /etc/hosts file on your machine (assuming OS X or *nix) and make sure you've setup 192.168.10.10 to point to someappname.app and in your ~/.homestead/Homestead.yaml make sure in the sites: section you have added

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

From within your ~/Homestead directory, type vagrant provision to make sure vagrant takes up the latest changes.

I think this should solve your problem.