在Laravel 5中找不到用户类别 [英] User Class not found in Laravel 5

查看:46
本文介绍了在Laravel 5中找不到用户类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题.我刚刚从4.2升级到Laravel 5,但是我刚复制到新Controllers文件夹的UserController不起作用.

它总是告诉我它没有破坏用户模型.

我的代码在复制后看起来如下:

UserController.php

 <?php使用App \ Transformer \ UserTransformer;UserController类扩展了ApiController{公共功能index(){App :: abort(403,'这是不允许的');}公共功能表演($ id){$ user =用户:: find($ id);if($ user-> access_token!= Input :: get('access_token')){返回不允许访问其他用户的数据";}别的{$ result = $ this-> respondWithItem($ user,new UserTransformer);返回$ result;}}公共函数getID(){$ user = User :: where('email','=',Input :: get('email'))-> first();if($ user-> access_token!= Input :: get('access_token')){返回不允许访问其他用户的数据";}别的{$ result = $ user-> id;返回$ result;}}公共功能寄存器(){$ user =新用户;$ name =输入:: get('name');$ email =输入::: get('email');$ password =输入::: get('password');if($ name!= null AND $ email!= null AND $ password!= null){if(User :: where('email','=',Input :: get('email'))-> exists()){返回使用此电子邮件的用户已存在";}别的{$ user-> name = $ name;$ user-> email = $ email;$ user->密码= $密码;$ user-> save();返回成功";}}别的{返回所有字段均为必填";}}公共功能login(){$ user = User :: where('email','=',Input :: get('email'))-> first();if($ user!= null){if($ user-> password == Input :: get('password')){$ then = $ user->时间;$ now = strtotime("now");$ thenTimestamp = strtotime($ then);$ difference = $ now-$ thenTimestamp;if((空($ user-> date)AND空($ user-> time))OR($ difference> = 10800)OR($ user-> date!= date('dmY'))){$ 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();}返回$ user-> access_token;}别的返回用户名或密码错误";}别的返回用户名或密码错误";}函数getGUID(){如果(function_exists('com_create_guid')){返回com_create_guid();}别的{mt_srand((double)microtime()* 100000);$ charid = strtoupper(md5(uniqid(rand(),true))));$连字符= chr(45);$ uuid =substr($ charid,0,8).$连字符.substr($ charid,8,4).$连字符.substr($ charid,12,4).$连字符.substr($ charid,16,4).$连字符.substr($ charid,20,12);返回$ uuid;}}公共功能check(){$ user = User :: where('access_token','=',Input :: get('access_token'))-> first();if($ user!= null){}别的返回无会话";}} 

我没有给Controller命名空间,而User Model就是开箱即用的.UserTransformer确实包含用户模型.这里是原始副本:

 <?php使用应用程序\用户;使用League \ Fractal \ TransformerAbstract;类UserTransformer扩展TransformerAbstract{/***将此项目对象转换为通用数组** @返回数组*/公共功能转换(用户$ user){返回 ['id'=>(int)$ user-> id,'名称'=>$ user->名称,'email'=>$ user->电子邮件,'电话'=>$ user-> img,'mobile'=>$ user->移动,'地址'=>$ user->地址,];}} 

这就是我需要的所有功能.

但是现在在Laravel 5中,UserController确实告诉我找不到User类.我试图将类似使用应用程序/用户"或使用应用程序/Http/Controllers/用户"的行添加到UserController中.这没用.我不确定为什么自从包含变压器后,我没有收到任何错误,但用户无法正常工作.我还尝试了不同的使用方法,将用户模型包含到Transfomer中,因为虽然可能会出现错误,但我没有可用的版本.

我希望有人能帮助我.

解决方案

让我们澄清一些可能会干扰控制器加载的事情.

路由:默认情况下,laravel随附一个名为 RouteServiceProvider 的提供程序,该提供程序位于 app \ providers 中.如果查看此文件,将会发现有一个名为 $ namespace 的变量.如果您想在 routes.php 文件中获得更多清晰"的路由,这将非常有用.例如,将 $ namespace ='App \ Http \ Controllers'转换为...

  Route :: get('welcome','App \ Http \ Controllers \ HomeController @ welcome'); 

对此...

  Route :: get('welcome','HomeController @ welcome'); 

是的,写规则的一个简短选择.默认情况下, $ namespace 的值设置为 {Application Name} \ Http \ Controllers

控制器:如果您的laravel安装正在与 RouteServiceProvider 配合使用,则将永远找不到您的控制器,因为路由会在先前声明的命名空间之前添加前缀.那么,我应该在控制器中使用哪个名称空间?有两种选择:

  1. 遵循laravel 5的约定,将您的控制器放在 app/Http/Controllers/文件夹中.然后,将名称空间声明为默认名称:

     命名空间{Application Name} \ Http \ Controllers; 

    并将其设置在 routes.php 上,例如:

     //天气是控制器路线:: get('foo','UserController @ index');//天气是一种资源路线:: resource('api-user','UserController'); 

    请注意,名称空间结构与它所在的文件夹结构(Http/Controllers/)相匹配

  2. 从providers文件夹中删除 RouteServiceProvider 并将其从 config/app.php 文件中的providers数组中删除.然后,您可以使用绝对"名称空间,例如:

     命名空间{Application Name} \ Http \ Controllers;路线:: get('foo','Myapp \ Http \ Controllers \ UserController @ index'); 

应用程序名称和自动加载

您是否对自己提出疑问,为什么未在名称空间中添加 app 文件夹?看看composer.json文件:

 自动加载":{类图":[数据库"],"psr-4":{"MyCoolApp \\":"app/"}}, 

如果您注意到的话,您会看到有一个别名指向 app/文件夹.因此,for app将响应别名而不是 app \ Http \ Controllers .我不是这个主题的专家,希望有人帮我扩展这个解释,但是我已经读过 psr-4 就像一条规则,使您将命名空间与文件夹结构进行匹配.结果,如果您的控制器就像...

 命名空间MyCoolApp \ Http \ Classes; 

自动加载类期望您的控制器驻留在 app/Http/Classes/而不是 MyCoolApp/Http/Controllers

这就是找不到控制器的原因.

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.

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

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:

<?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.

I hope someone can help me.

解决方案

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...

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

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:

  1. 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/)

  2. Delete RouteServiceProvider from providers folder AND delete it from providers array in config/app.php file. Then, you can use "absolute" namespaces like:

    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:

"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...

namespace MyCoolApp\Http\Classes;

The autoload class expects that your controller resides at aapp/Http/Classes/ instead of MyCoolApp/Http/Controllers

That is a reason why a controller could not be found.

这篇关于在Laravel 5中找不到用户类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆