11
respostas

Ao adicionar o prefixo nas minhas rotas recebo "NotFoundHttpException", quando eu tiro o prefixo a rota funciona normal.

<?php

$router->get('/', function () use ($router) {
    return $router->app->version();
});

$router->group(['prefix' => 'api/'], function ($router) {
    $router->post('', 'SeriesController@store');
});
11 respostas

Olá, Danielle.

Sua configuração de prefixo está errada.

O correto seria:

$router->group(['prefix' => '/api'], function () use ($router) {
    $router->post('/series', 'SeriesController@store');
    // ...
});

Fiz a correção mais ainda está dando o mesmo erro:

NotFoundHttpException HTTP 404 Not Found Exception

Symfony\Component\HttpKernel\ExceptionNotFoundHttpException in /Users/danielle/projetos/lumen/api-users/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php (line 229) / protected function handleDispatcherResponse($routeInfo) { switch ($routeInfo[0]) { case Dispatcher::NOT_FOUND: throw new NotFoundHttpException; case Dispatcher::METHOD_NOT_ALLOWED: throw new MethodNotAllowedHttpException($routeInfo[1]); case Dispatcher::FOUND: return $this->handleFoundRoute($routeInfo); } in /Users/danielle/projetos/lumen/api-users/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php -> handleDispatcherResponse (line 169) if (isset($this->router->getRoutes()[$method.$pathInfo])) { return $this->handleFoundRoute([true, $this->router->getRoutes()[$method.$pathInfo]['action'], []]); } return $this->handleDispatcherResponse( $this->createDispatcher()->dispatch($method, $pathInfo) ); }); } catch (Throwable $e) { return $this->prepareResponse($this->sendExceptionToHandler($e)); } in /Users/danielle/projetos/lumen/api-users/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php -> Laravel\Lumen\Concerns{closure} (line 416) ->send($this->make('request')) ->through($middleware) ->then($then); } return $then($this->make('request')); } /* * Prepare the response for sending. * in /Users/danielle/projetos/lumen/api-users/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php -> sendThroughPipeline (line 171) } return $this->handleDispatcherResponse( $this->createDispatcher()->dispatch($method, $pathInfo) ); }); } catch (Throwable $e) { return $this->prepareResponse($this->sendExceptionToHandler($e)); } } in /Users/danielle/projetos/lumen/api-users/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php -> dispatch (line 108)

 * @param  SymfonyRequest|null  $request     * @return void     */    public function run($request = null)    {        $response = $this->dispatch($request);        if ($response instanceof SymfonyResponse) {            $response->send();        } else {            echo (string) $response;

Application->run() in /Users/danielle/projetos/lumen/api-users/public/index.php (line 28) | the client's browser allowing them to enjoy the creative| and wonderful application we have prepared for them.|*/$app->run();

Que url você está tentando acessar

localhost:8000/users

E onde você definiu essa rota /users, Danielle?

Nos exemplos que você mandou, a única rota definida era /api/series

Sim comecei outro projeto com lumen e criei users pra testar mais continuou com erro, mas o meu aquivo web.php do curso está assim, no caso subtitui series por livros.

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/

$router->get('/', function () use ($router) {
    return $router->app->version();
});

$router->group(['prefix' => '/api'], function () use ($router) {
    $router->post('/livros', 'LivrosController@store');
    $router->get('/livros', 'LivrosController@index');
    $router->get('/livros/{id}', 'LivrosController@show');
    $router->put('/livros/{id}', 'LivrosController@update');
    $router->delete('/livros/{id}', 'LivrosController@destroy');
});

Então, Danielle. Você está tentando acessar uma rota que não existe. Não tem nenhuma rota /users configurada.

Acho q vc n entendeu. Eu criei o projeto do curso esse q te mandei agora, dai deu o erro q mencionei. Entao configurei o lumen tudo de novo e criei essa rota users pra testar e continuou com o erro.

Realmente não estou entendendo, Danielle. Vamos do começo:

Se você continua com erro, me fala qual URL você está tentando acessar agora, e como está seu arquivo de rotas agora.

localhost:8000/livros

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/

$router->get('/', function () use ($router) {
    return $router->app->version();
});

$router->group(['prefix' => '/api'], function () use ($router) {
    $router->post('/livros', 'LivrosController@store');
    $router->get('/livros', 'LivrosController@index');
    $router->get('/livros/{id}', 'LivrosController@show');
    $router->put('/livros/{id}', 'LivrosController@update');
    $router->delete('/livros/{id}', 'LivrosController@destroy');
});

Ok, Danielle. De novo: A rota que você está tentando acessar não existe.

Você tem configurada a rota /api/livros. Somente /livros não, entende?

Se você acessar localhost:8000/api/livros deve funcionar.

;-)

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software