0

I am new to Laravel and lumen & trying to built an api for User creation, deletion, updating and viewing single user information. There is a UserController and its functions. Routes are defined in routes.php file. But when I send any request, it shows "Sorry, the page you are looking for could not be found". Please help.

I have tried A solution on stack overflow for the same problem but it did not helped me.

My Controller Code is as below

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
class UserController extends Controller
{
    
    protected $fillable = [
        'name', 'email', 'phone', 'password', 'password_confirmation', 'address'
    ];    
    public function showAllUsers()
    {
        return response()->json(User::all());
    }

    public function showOneUser($id)
    {
        echo "string";
        return response()->json(User::find($id));
    }

    public function create(Request $request)
    {
        $user = User::create($request->all());

        return response()->json($user, 201);
    }

    public function update($id, Request $request)
    {
        $user = User::findOrFail($id);
        $user->update($request->all());

        return response()->json($user, 200);
    }

    public function delete($id)
    {
        User::findOrFail($id)->delete();
        return response('Deleted Successfully', 200);
    }

}

Routes are defined as follows

<?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.
|
*/

$app->get('/', function () use ($app) {
    return $app->welcome();
});
  $app->get('user',  ['uses' => 'UserController@showAllUserControllers']);

  $app->get('user/{id}', ['uses' => 'UserController@showOneUserController']);

  $app->post('user', ['uses' => 'UserController@create']);

  $app->delete('user/{id}', ['uses' => 'UserController@delete']);

  $app->put('user/{id}', ['uses' => 'UserController@update']);

As suggested by Provided Answer I have made the following changes in public/index.php

<?php

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| First we need to get an application instance. This creates an instance
| of the application / container and bootstraps the application so it
| is ready to receive HTTP / Console requests from the environment.
|
*/

$app = require __DIR__.'/../bootstrap/app.php';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/

// $app->run();
// $app->run($app->request);
$app->run(
    $app->make('request')
);

1 Answers1

0

I had a similar issue, googled a lot and tried the same things that I saw in this question. In the end, the root cause was a typo in the function name as suggested by @porloscerros

In any case, for anyone facing the same problem, this is the configuration that actually worked:

public/index.php

...
$app->run();

routes

...
$app->get('user',  'UserController@showAllUsers');
...

UserControllers.php

...
    public function showAllUsers()
    {
        return response()->json(User::all());
    }
...