【问题标题】:Laravel 5 UserController Throwing Error FatalErrorExceptionLaravel 5 UserController 抛出错误 FatalErrorException
【发布时间】:2015-05-25 11:20:54
【问题描述】:

我在使用 laravel 5.0 控制器时遇到问题。我正在构建触发 UserController 的 RESTFul 函数。

这里是我的routes.php

Route::post('user/create', 'UserController@create');

我使用此命令使用 cURL 发布输入用户

curl -d 'fname=randy&lname=tan&id_location=1&email=randy@randytan.me&password=randytan&remember_token=Y&created_at=2015-03-03' localhost:8000/user/create

这是我在 UserController 上的创建方法

public function create()
    {
        //function to create user.
        $userAccounts = new \App\User;
        $userAccounts->fname = Request::post('fname');
        $userAccounts->lname = Request::post('lname');
        $userAccounts->id_location = Request::post('id_location');
        $userAccounts->email = Request::post('email');
        $userAccounts->password = Hash::make(Request::post('password'));
        $userAccounts->created_at = Request::post('created_at');

        $userAccounts->save();

        return Response::json(array(
                'error' => false,
                'user'  => $userAccounts->fname . " " . $userAccounts->lname
            ), 200);

    }

但不知何故,我触发了 cURL 命令,它抛出 FatalErrorException,上面写着 Class 'App\User' not found

我已尝试更改语法$userAccounts = new User;,但仍然没有运气,

有什么想法吗?

之前谢谢。

================================ 更新

这里是我的控制器的所有源代码

<?php namespace randytan\Http\Controllers;

use randytan\Http\Requests;
use randytan\Http\Controllers\Controller;

use Illuminate\Http\Request;

class UserController extends Controller {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        //function to get index of user. basically get the user token.
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //function to create user.
        $userAccounts = new App\User;
        $userAccounts->fname = Request::post('fname');
        $userAccounts->lname = Request::post('lname');
        $userAccounts->id_location = Request::post('id_location');
        $userAccounts->email = Request::post('email');
        $userAccounts->password = Hash::make(Request::post('password'));
        $userAccounts->created_at = Request::post('created_at');

        $userAccounts->save();

        return Response::json(array(
                'error' => false,
                'user'  => $userAccounts->fname . " " . $userAccounts->lname
            ), 200);

    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        //function store user acounts
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //function to get user resources
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //function to edit the user.
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        //function to update the user accounts
        $userAccountDetails = User::where('id', $id)->get();

        /* get details of user from the GET parameter */
        $username = "randy";

        if($userAccountDetails){
            var_dump("123");
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     * Update Connection Between Two User
     *
     * @param  int  $id
     * @return Response
     */
    public function follows($id)
    {
        //
    }

}

【问题讨论】:

  • Laravel 5 使用 PSR-4 自动加载器。您是否将 use App\User 放在代码文件的顶部?您是否移动了用户模型?您还在 App 的全局命名空间中查找。尝试从开头删除 \
  • 是否存在App目录中?如果是,请发布完整的控制器代码,可能错误与其他行有关。
  • @Zarathuztra 不,我没有将 App\User 放在文件顶部,也没有将用户模型移动到另一个文件夹。
  • @MarcinNabiałek 我发布了完整的源代码。请仔细查看

标签: php laravel controller namespaces laravel-5


【解决方案1】:

查看您的代码和命名空间,您的 User 类可能在 randytan 命名空间中,因此您应该使用:

$userAccounts = new randytan\User;

创建您的用户。您的 update 方法也是如此,因此最好添加

use randytan\User;

之前:

class UserController

现在你可以在这个类的任何地方使用User

【讨论】:

  • 配置的命名空间是否覆盖了我当前的app 路径?
  • @randytan 看来您已将默认 App 命名空间更改为 randytan,所以您通常在 App 命名空间中拥有的所有内容都在 randytan 命名空间中拥有
  • 感谢 Marcin,对于函数 Input::,当我从一个方法触发时,它也会抛出 notFound,我是否也应该重写它?
  • @randytan 对于其他类/门面,例如 Request,可能是 Input,您应该在您的类之前导入它们,例如 use Input; 或使用 \Input::something() 语法
  • 感谢您的解释。现在了解为什么所有参数都无法正常工作。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-21
  • 1970-01-01
  • 2018-03-24
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
相关资源
最近更新 更多