[英]Laravel, namespaces and PSR-4
I'm trying to set up PSR-4 within a new Laravel 4 application, but I'm getting some troubles achieving what I want when it comes to build controllers. 我正在尝试在新的Laravel 4应用程序中设置PSR-4,但是在构建控制器时遇到一些麻烦。
Here's what I have now : 这是我现在所拥有的:
namespace MyApp\Controllers\Domain;
class DomainController extends \BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = \View::make('domain.home');
}
}
I'm not so fond of using \\View
, \\Config
, \\Whatever
to use Laravel's classes. 我不太喜欢使用
\\View
, \\Config
和\\Whatever
使用Laravel的类是什么。 So I was wondering if I could put a use Illuminate\\View;
所以我想知道是否可以
use Illuminate\\View;
to be able to use View::make
without putting a \\
. 无需使用
\\
就可以使用View::make
。
Unfortunately, while doing this, I'm getting the following error : Class 'Illuminate\\View' not found
. 不幸的是,在执行此操作时,出现以下错误:
Class 'Illuminate\\View' not found
。
Could somebody help with this please ? 有人可以帮忙吗?
Assuming BaseController.php has a namespace of MyApp\\Controllers\\Domain 假设BaseController.php具有MyApp \\ Controllers \\ Domain的命名空间
namespace MyApp\Controllers\Domain;
use View;
class DomainController extends BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = View::make('domain.home');
}
}
If BaseController.php has other namespace, ie MyApp\\Controllers 如果BaseController.php具有其他名称空间,即MyApp \\ Controllers
namespace MyApp\Controllers\Domain;
use MyApp\Controllers\BaseController;
use View;
class DomainController extends BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = View::make('domain.home');
}
}
If, for instance, you controller needs to use another base class from Laravel, lets say Config
. 例如,如果您的控制器需要使用Laravel中的另一个基类,请说
Config
。
namespace MyApp\Controllers\Domain;
use MyApp\Controllers\BaseController;
use View;
use Config;
class DomainController extends BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = View::make('domain.home')->withName(Config::get('site.name'));
}
}
The problem in your case is that View
is not located in Illuminate
namespace but in Illuminate\\View
namespace, so correct import would be not: 您遇到的问题是
View
不在Illuminate
名称空间中,而是在Illuminate\\View
名称空间中,因此正确的导入将不是:
use Illuminate\View;
but 但
use Illuminate\View\View;
You can look at http://laravel.com/api/4.2/ to find out which namespace is correct for class you want to use 您可以查看http://laravel.com/api/4.2/以找出哪个名称空间对于您要使用的类是正确的
The use of View::make() takes advantage of the Laravel facades. 使用View :: make()可充分利用Laravel外观。 To properly reference the facade, instead of directly referencing the class that gets resolved out of the iOC container, I would use the following:
为了正确地引用外观,而不是直接引用从iOC容器中解析出来的类,我将使用以下内容:
use Illuminate\Support\Facades\View;
This will reference the View facade that is being used when calling View::make() 这将引用在调用View :: make()时使用的View门面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.