流明中Application.php中的NotFoundHttpException

时间:2019-06-15 17:33:09

标签: php laravel lumen

我是Laravel和lumen的新手,并尝试构建用于创建,删除,更新和查看单个用户信息的api。有一个UserController及其功能。路由在routes.php文件中定义。但是,当我发送任何请求时,它会显示“抱歉,找不到您要查找的页面”。请帮助。

我尝试了A solution on stack overflow for the same problem,但没有帮助我。

我的控制器代码如下

<?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);
    }

}

路线定义如下

<?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']);

如提供的答案所建议,我在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 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,在 google 上搜索了很多,并尝试了我在这个问题中看到的相同内容。最后,根本原因是@porloscerros 建议的函数名称输入错误

无论如何,对于任何面临同样问题的人来说,这是实际有效的配置:

public/index.php

...
$app->run();

路线

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

UserControllers.php

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