简体   繁体   中英

Laravel 5.0 - Calling the use in basecontroller?

Here is my Controller.php that all the other Controller extend from. 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.

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!

I recommend you to read this page from the php manual: 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. However I expect a diffrent object type.

Hope you will understand it better now. Consider a good 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". Only pressing a few buttons on you're keyboard and you're done :)

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. Laravel 5.1 is LTS version so has longer support!

Unfortunately that's not possible, because you cannot "inherit" use statements. The note below is taken from the PHP docs on Using namespaces: 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.


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. These statements are helpers to avoid using fully qualified namespaces within the code, which would generally make the code too verbose and less readable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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