phplaravel.htaccesspostmanlumen-5.4

The Rest API not working in postman when requested by url


I have start server by this cmd in the console, and it is working fine

php -S localhost:8000 -t public

When i call the API in postman for the lumen like this:

localhost:8000/GitHub/twinfield/public/index.php/user

The API work for insert but when i call like this:

localhost:8000/GitHub/twinfield/user

The error is given below:

(1/1) NotFoundHttpException in RoutesRequests.php (line 226) at Application->handleDispatcherResponse(array(0)) in RoutesRequests.php (line 164) at Application->Laravel\Lumen\Concerns{closure}() in RoutesRequests.php (line 413) at Application->sendThroughPipeline(array(), object(Closure)) in RoutesRequests.php (line 166) at Application->dispatch(null) in RoutesRequests.php (line 107) at Application->run() in index.php (line 28)

My routes file in routes/web.php:

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

$router->get('user','userController@index');
$router->get('user/{id}','userController@getuser');      
$router->post('user','userController@createuser');
$router->post('user/{id}','userController@updateuser');    
$router->delete('user/{id}','userController@deleteuser');

I have already try to solve like this in public/index.php without success.

$request = Illuminate\Http\Request::capture();
$app->run($request);

I am working in localhost with Xampp. The database is MySQL and PHP version is 7.1.


Solution

  • Sir, you forget the url link or may be missed. Its always when we start the server(In the laravel the command is php artisan serve), It always belongs to that path from where it is called.

    Like in your case, your path is xampp/htdocs/GItHub/twinfield where the lumen reside(i suppose).

    localhost:8000/GitHub/twinfield/public/index.php/user
    

    so if you called like this:

    localhost:8000/GitHub/twinfield/user
    

    It is never worked because you defined the route:

    $router->get('user','userController@index'); 
    $router->post('user','userController@createuser');
    

    thats why you have to use only the name of the route and type. Open postman and add this line with type->get:

    localhost:8000/user
    

    you will get all users in the DB. If you add this line with the type->post.

    localhost:8000/user
    

    Than you should post the body with the field of the Table in which you want to save.