Laravel 5 UserController抛出错误FatalErrorException

时间:2015-03-22 11:32:05

标签: php laravel controller namespaces laravel-5

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

这是我的routes.php

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

我使用此命令

使用cURL对输入用户进行POST
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)
    {
        //
    }

}

1 个答案:

答案 0 :(得分:0)

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

$userAccounts = new randytan\User;

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

,情况会更好
use randytan\User;

之前:

class UserController

现在您可以在此课程的任何位置使用User