我正试图在一个新的Laravel 4应用程序中设置PSR-4,但在构建控制器时,我遇到了一些问题。
这是我现在拥有的:
namespace MyApp'Controllers'Domain;
class DomainController extends 'BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = 'View::make('domain.home');
}
}
我不太喜欢使用'View
、'Config
、'Whatever
来使用Laravel的类。所以我想知道我是否可以放一个use Illuminate'View;
来使用View::make
而不放'
。
不幸的是,在执行此操作时,我收到以下错误:Class 'Illuminate'View' not found
。
有人能帮忙吗?
您的案例中的问题是View
不在Illuminate
命名空间中,而是在Illuminate'View
命名空间中,因此正确的导入不是:
use Illuminate'View;
但是
use Illuminate'View'View;
你可以看看http://laravel.com/api/4.2/要找出要使用的类的正确名称空间
假设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');
}
}
如果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');
}
}
例如,如果控制器需要使用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'));
}
}
View::make()的使用利用了Laravel外墙的优势。为了正确地引用facade,而不是直接引用从iOC容器中解析的类,我将使用以下内容:
use Illuminate'Support'Facades'View;
这将引用调用View::make()