流明中 Application.php 中的 NotFoundHttpException

Posted

技术标签:

【中文标题】流明中 Application.php 中的 NotFoundHttpException【英文标题】:NotFoundHttpException in Application.php in lumen 【发布时间】:2019-10-29 22:44:10 【问题描述】:

我是 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')
);

【问题讨论】:

您是如何发送请求的? 通过邮递员 ctr+alt+c 并在控制台中检查请求是否转到正确的 url 你的路由中控制器的方法不对:showAllUserControllersshowOneUserController应该是showAllUsersshowOneUser 这是导致错误的原因吗?我认为这是一个错误,但我不确定它是否会导致 404。很高兴贡献 【参考方案1】:

我有一个类似的问题,用谷歌搜索了很多,并尝试了我在这个问题中看到的相同的东西。最后,根本原因是@porloscerros 建议的函数名称中的拼写错误

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

public/index.php

...
$app->run();

路线

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

UserControllers.php

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

【讨论】:

以上是关于流明中 Application.php 中的 NotFoundHttpException的主要内容,如果未能解决你的问题,请参考以下文章

更改流明中的 Faker 本地化

如何通过流明中的作曲家安装照明/邮件

流明无法开箱即用

流明文件上传测试:请求中不存在文件

Laravel 流明 |从数组中获取单个值

带有标题集的流明/laravel中的CORS问题