简体   繁体   English

Laravel 5.0-在basecontroller中调用使用吗?

[英]Laravel 5.0 - Calling the use in basecontroller?

Here is my Controller.php that all the other Controller extend from. 这是我的Controller.php,所有其他Controller都从此扩展。 I was hoping to do that 我希望那样做

<?php

namespace App\Http\Controllers;

use App\User;
use App;
use URL;
use App\City;

use Illuminate\Foundation\Bus\DispatchesCommands;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;

abstract class Controller extends BaseController {

    use DispatchesCommands, ValidatesRequests;

}

But looks like it doesn't work. 但是看起来不起作用。 I need to call use App\\User in each controller that uses it even if it is extending the basecontroller. 我需要在使用它的每个控制器中调用use App\\User ,即使它扩展了basecontroller。

This doesn't work: 这不起作用:

<?php
namespace App\Http\Controllers;

class HomeController extends Controller { .... }

But this does: 但这确实是:

namespace App\Http\Controllers;

use App\User;
use App;
use URL;
use App\City;

class HomeController extends Controller { .... }

Is this normal? 这正常吗? Does it has to be that painful :-) There are some Classes used in every Controller, it is just a bit surprising I need to call them every time. 它是否一定很痛苦:-)每个控制器中都使用了一些类,所以每次都需要调用它们有点令人惊讶。

Note: I am migrating from 4.2 and want to go with namespaces Thanks! 注意:我正在从4.2迁移,并想使用命名空间谢谢!

I recommend you to read this page from the php manual: http://php.net/manual/en/language.namespaces.importing.php 我建议您从php手册中阅读此页面: http : //php.net/manual/en/language.namespaces.importing.php

Consider the following code: 考虑以下代码:

<?php

namespace App;

use App\User;

class MyClass extends Controller
{
    public function __construct()
    {
        new User(); // we refer to the App\User
    }
}

//another file
<?php

namespace App\AnotherNamespace;

class MySecondClass extends Controller
{
    public function __construct()
    {
        new User(); // I refer to App\AnotherNamespace\User
    }
}

In the above example I use both User as a reference. 在上面的示例中,我将两个User都用作参考。 However I expect a diffrent object type. 但是我期望一个不同的对象类型。

Hope you will understand it better now. 希望您现在能更好地理解它。 Consider a good IDE!!! 考虑一个好的IDE! That will be the best solution for you. 那将是您的最佳解决方案。 It's incredibly usefull to press "ctrl -> space" and see the results when you type "User". 按下“ Ctrl->空格”并在键入“用户”时查看结果非常有用。 Only pressing a few buttons on you're keyboard and you're done :) 只需按一下键盘上的几个按钮,就可以完成:)

PS. PS。 If you're coming from 4.2 I recommend you to update directly to 5.1 because also 5.0 isn't supported anymore. 如果您来自4.2,我建议您直接更新到5.1,因为不再支持5.0。 Laravel 5.1 is LTS version so has longer support! Laravel 5.1是LTS版本,因此具有更长的支持!

Unfortunately that's not possible, because you cannot "inherit" use statements. 不幸的是,这是不可能的,因为您不能“继承” use语句。 The note below is taken from the PHP docs on Using namespaces: Aliasing/Importing : 以下注释摘自有关使用名称空间的PHP文档:Aliasing / Importing

Note: 注意:

Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules. 导入规则是基于文件的,这意味着包含的文件将不会继承父文件的导入规则。

So you need to declare use statements in each file as needed. 因此,您需要根据需要在每个文件中声明use语句。


Importing is done at compile time and not runtime. 导入是在编译时而不是在运行时完成的。 This is not something specific to PHP, for example Java import statements and C# using statements are not "inherited" because they are not compiled, they are just used by the compiler to resolve namespaces when compiling. 这不是特定于PHP的东西,例如Java import语句和C# using语句未“继承”,因为它们没有被编译,编译器仅在编译时使用它们来解析名称空间。 These statements are helpers to avoid using fully qualified namespaces within the code, which would generally make the code too verbose and less readable. 这些语句有助于避免在代码中使用完全限定的名称空间,这通常会使代码过于冗长且可读性差。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM