Laravel 5 UserController 抛出错误 FatalErrorException
Laravel 5 UserController Throwing Error FatalErrorException
我在使用 laravel 5.0 控制器时遇到问题。我正在构建 RESTFul 触发 UserController 的函数。
这是我的routes.php
Route::post('user/create', 'UserController@create');
我POST输入用户使用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)
{
//
}
}
查看您的代码和命名空间,您的 User
class 可能在 randytan
命名空间中,因此您应该使用:
$userAccounts = new randytan\User;
创建您的用户。您的 update
方法也是如此,因此最好添加
use randytan\User;
之前:
class UserController
现在您可以在 class
中的任何地方使用 User
我在使用 laravel 5.0 控制器时遇到问题。我正在构建 RESTFul 触发 UserController 的函数。
这是我的routes.php
Route::post('user/create', 'UserController@create');
我POST输入用户使用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)
{
//
}
}
查看您的代码和命名空间,您的 User
class 可能在 randytan
命名空间中,因此您应该使用:
$userAccounts = new randytan\User;
创建您的用户。您的 update
方法也是如此,因此最好添加
use randytan\User;
之前:
class UserController
现在您可以在 class
中的任何地方使用User