问题
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.
回答1:
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:
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');
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.
来源:https://stackoverflow.com/questions/28420058/user-class-not-found-in-laravel-5