我遇到以下问题。我刚刚从4.2升级到Laravel 5,但我刚复制到新的Controllers文件夹的UserController不起作用。
它总是告诉我,它不会破坏用户模型。
当我复制我的代码时,它看起来如下:
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中添加类似"使用App/User"或"使用App/Http/Controllers/User"的行。它不起作用。我不知道为什么,因为我已经包括了我的变压器,我没有得到任何错误,但用户不工作。我也尝试了不同的使用方法来将用户模型包含到转换器中,因为我认为可能会有错误,但我没有得到一个工作版本。
我希望有人能帮助我。
让我们澄清一些可能干扰加载控制器的事情。
路由:默认情况下,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
,那么您的控制器将永远找不到,因为路由前缀是之前声明的命名空间。那么,我应该在控制器中使用哪个命名空间?有两种选择:
按照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/)匹配
从providers文件夹中删除
RouteServiceProvider
,并从config/app.php
文件中的providers数组中删除。然后,您可以使用"绝对"名称空间,如:namespace {Application Name}'Http'Controllers; Route::get('foo', 'Myapp'Http'Controllers'UserController@index');
应用程序名称和自动加载
您有没有问过自己为什么app
文件夹没有添加到命名空间中?看看composer.json文件:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"MyCoolApp''": "app/"
}
},
如果您注意到,您将看到有一个指向app/
文件夹的别名。因此,for应用程序将响应别名,而不是app'Http'Controllers
。我不是这个主题的专家,希望有人帮我扩展一下这个解释,但我读到psr-4
就像一个规则,让你将命名空间与文件夹结构相匹配。因此,如果你的控制器像。。。
namespace MyCoolApp'Http'Classes;
autoload类期望您的控制器驻留在app/Http/Classes/
而不是MyCoolApp/Http/Controllers
这就是为什么找不到控制器的原因。