在 Laravel 5 中找不到用户类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28420058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
User Class not found in Laravel 5
提问by Sebi55
I have got the following Problem. I have just upgraded to Laravel 5 from 4.2 but my UserController which I first just copied to the new Controllers folder does not work.
我有以下问题。我刚刚从 4.2 升级到 Laravel 5,但我第一次复制到新 Controllers 文件夹的 UserController 不起作用。
It always tells me that it does not fiend the User Model.
它总是告诉我它并不讨厌用户模型。
My Code looked like the following when I copied it:
当我复制它时,我的代码如下所示:
UserController.php
用户控制器.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";
}
}
I did not have the the Controller namespaced and the User Model just worked out of the box. The UserTransformer does have the User Model included. Here the original copy:
我没有控制器命名空间,用户模型只是开箱即用。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,
];
}
}
That is everything that I needed to have the functions to work.
这就是我让功能正常工作所需的一切。
But now in Laravel 5 the UserController does tell me that the User class is not found. I have tried to add lines like this "use App/User" or "use App/Http/Controllers/User" to the UserController. It did not work. I am not sure why since I have got my Transformer included I do not get any error for this but the User does not work. I have also tried different use methods to include the User Model into the Transfomer since I though there might be an error but I do not get a working version.
但是现在在 Laravel 5 中 UserController 确实告诉我找不到 User 类。我曾尝试向 UserController 添加类似“使用 App/User”或“使用 App/Http/Controllers/User”这样的行。这没用。我不知道为什么,因为我已经包含了我的 Transformer,所以我没有收到任何错误,但用户无法工作。我还尝试了不同的使用方法将用户模型包含到 Transfomer 中,因为我虽然可能会出现错误但我没有得到工作版本。
I hope someone can help me.
我希望有一个人可以帮助我。
回答by manix
Let's clarify just some things that could be interfering in loading the controller.
让我们澄清一些可能会干扰加载控制器的事情。
The route: by default, laravel comes with a provider called RouteServiceProvider
found at app\providers
. If you take a look to this file you will find that there is a variable named $namespace
. This is very usefull if you want get more "clear" routes at routes.php
file. For example, having $namespace = 'App\Http\Controllers'
you convert this...
路线:默认情况下,laravel 带有一个名为RouteServiceProvider
found at的提供者app\providers
。如果您查看此文件,您会发现有一个名为$namespace
. 如果您想在routes.php
文件中获得更多“清晰”的路线,这将非常有用。例如,让$namespace = 'App\Http\Controllers'
你转换这个......
Route::get('welcome', 'App\Http\Controllers\HomeController@welcome');
to this...
到这...
Route::get('welcome', 'HomeController@welcome');
So yes, a short choice for write your rules. By default, the value of $namespace
is set to {Application Name}\Http\Controllers
所以是的,编写规则的简短选择。默认情况下,的值$namespace
设置为{Application Name}\Http\Controllers
The controller: If you laravel installation is working with the RouteServiceProvider
then your controller never will be found, because the routes prefix the namespace previously declared. So, which namespace should I use in controllers? there are two options:
控制器:如果您使用 laravel 安装,RouteServiceProvider
那么您的控制器将永远不会被找到,因为路由前缀是先前声明的命名空间。那么,我应该在控制器中使用哪个命名空间?有两种选择:
Following the conventions of laravel 5 putting your controller at
app/Http/Controllers/
folder. Then, declare the namespace as default:namespace {Application Name}\Http\Controllers;
and setting it at
routes.php
like:// weather is a controller Route::get('foo', 'UserController@index'); // weather is a resource Route::resource('api-user', 'UserController');
Notice that the namespace structure match the same folder structe where it is resides (Http/Controllers/)
Delete
RouteServiceProvider
from providers folder AND delete it from providers array inconfig/app.php
file. Then, you can use "absolute" namespaces like:namespace {Application Name}\Http\Controllers; Route::get('foo', 'Myapp\Http\Controllers\UserController@index');
遵循 laravel 5 的约定,将您的控制器放在
app/Http/Controllers/
文件夹中。然后,将命名空间声明为默认值:namespace {Application Name}\Http\Controllers;
并将其设置为
routes.php
:// weather is a controller Route::get('foo', 'UserController@index'); // weather is a resource Route::resource('api-user', 'UserController');
请注意,命名空间结构与其所在的文件夹结构(Http/Controllers/)匹配
删除
RouteServiceProvider
从供应商文件夹,并从供应商阵列删除config/app.php
文件。然后,您可以使用“绝对”命名空间,例如:namespace {Application Name}\Http\Controllers; Route::get('foo', 'Myapp\Http\Controllers\UserController@index');
Application name and autoload
应用程序名称和自动加载
did you question yourself why app
folder is not added at namespace? Take a look the composer.json file:
你有没有问过自己为什么app
没有在命名空间中添加文件夹?看一下 composer.json 文件:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"MyCoolApp\": "app/"
}
},
If you note, you will see that there is an alias that points out to app/
folder. So, for app will response to the alias instead of app\Http\Controllers
.
I am no expert in this topic, hope somebody give me hand to expand this explanation, but I have read that psr-4
is like a rule that make you match the namespace with the folder structure. As result, if your controller is like...
如果您注意,您会看到有一个别名指向app/
文件夹。因此,for app 将响应别名而不是app\Http\Controllers
. 我不是这个主题的专家,希望有人能帮我扩展这个解释,但我读过这psr-4
就像一条规则,让你将命名空间与文件夹结构相匹配。结果,如果您的控制器类似于...
namespace MyCoolApp\Http\Classes;
The autoload class expects that your controller resides at aapp/Http/Classes/
instead of MyCoolApp/Http/Controllers
自动加载类期望您的控制器驻留在一个app/Http/Classes/
而不是MyCoolApp/Http/Controllers
That is a reason why a controller could not be found.
这就是无法找到控制器的原因。