我有以下问题。
我刚刚从 4.2 升级到 Laravel 5,但是我首先复制到新的 Controllers 文件夹的 UserController 不起作用。
它总是告诉我它不适合用户模型。
当我复制它时,我的代码如下所示:
用户控制器.php
<?php
use App\Transformer\UserTransformer;
class UserController extends ApiController
{
public function index()
{
App::abort(403, 'This is not allowed');
}
public function show($id)
{
$user = User::find($id);
if($user->access_token!=Input::get('access_token')){
return "It is not allowed to access data from other users";
}else{
$result = $this->respondWithItem($user, new UserTransformer);
return $result;
}
}
public function getID()
{
$user = User::where('email', '=', Input::get('email'))->first();
if($user->access_token!=Input::get('access_token')){
return "It is not allowed to access data from other users";
}else{
$result = $user->id;
return $result;
}
}
public function register()
{
$user = new User;
$name = Input::get('name');
$email = Input::get('email');
$password = Input::get('password');
if($name != null AND $email != null AND $password != null){
if(User::where('email', '=', Input::get('email'))->exists()){
return "A user with this email already exists";
}else{
$user->name = $name;
$user->email = $email;
$user->password = $password;
$user->save();
return "Success";
}
}else{
return "All fields are required";
}
}
public function login(){
$user = User::where('email', '=', Input::get('email'))->first();
if($user != null){
if($user->password==Input::get('password')){
$then =$user->time;
$now = strtotime("now");
$thenTimestamp = strtotime($then);
$difference = $now - $thenTimestamp;
if((empty($user->date) AND empty($user->time))OR($difference >=10800)OR($user->date!=date('d.m.Y'))){
$user->access_token=self::getGUID();
$timestamp = time();
$user->date=date("d.m.Y",$timestamp);
$timestamp = strtotime("now");
$user->time=date('H:i', $timestamp);
$user->save();
}
return $user->access_token;
}
else
return "Wrong username or password";
}else
return "Wrong username or password";
}
function getGUID(){
if (function_exists('com_create_guid')){
return com_create_guid();
}else{
mt_srand((double)microtime()*100000);
$charid = strtoupper(md5(uniqid(rand(), true)));
$hyphen = chr(45);
$uuid =
substr($charid, 0, 8).$hyphen
.substr($charid, 8, 4).$hyphen
.substr($charid,12, 4).$hyphen
.substr($charid,16, 4).$hyphen
.substr($charid,20,12);
return $uuid;
}
}
public function check(){
$user = User::where('access_token', '=', Input::get('access_token'))->first();
if($user != null){
}else
return "no session";
}
}
我没有命名空间控制器,用户模型只是开箱即用。 UserTransformer 确实包含用户模型。这里是原件:
<?php
use App\User;
use League\Fractal\TransformerAbstract;
class UserTransformer extends TransformerAbstract
{
/**
* Turn this item object into a generic array
*
* @return array
*/
public function transform(User $user)
{
return [
'id' => (int) $user->id,
'name' => $user->name,
'email' => $user->email,
'phone' => $user->img,
'mobile' => $user->mobile,
'address' => $user->address,
];
}
}
这就是我让这些功能正常工作所需的一切。
但现在在 Laravel 5 中,UserController 确实告诉我找不到 User 类。我尝试向 UserController 添加类似“use App/User”或“use App/Http/Controllers/User”的行。它不起作用。
我不知道为什么,因为我已经包含了变压器,所以我没有收到任何错误,但用户不起作用。
我还尝试了不同的使用方法将用户模型包含到 Transfomer 中,因为我虽然可能会出现错误,但我没有得到工作版本。
我希望有一个人可以帮助我。