我在使用laravel 5.0控制器时遇到问题。我正在构建触发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)
{
//
}
}
查看您的代码和名称空间,您的User
类可能在randytan
名称空间中,因此您应该使用:
$userAccounts = new randytan'User;
创建您的用户。update
方法也是如此,所以最好添加
use randytan'User;
之前:
class UserController
现在您可以在这个类的任何位置使用User