php 从 yii2 中的模块控制器设置布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31851718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set layout from module controller in yii2
提问by Maulana Yusuf
I have three layouts in my layouts folder in main views folder. I added a module called subDomain. In my subDomain module I have a Controller called HomeController. In HomeController there is an action called getDomain()
.
我在主视图文件夹的布局文件夹中有三个布局。我添加了一个名为 subDomain 的模块。在我的子域模块中,我有一个名为 HomeController 的控制器。在 HomeController 中有一个名为getDomain()
.
In the getDomain()
action I want to change the main layout to getDomainLayout
. But there is an error when I use a code:
在getDomain()
操作中,我想将主布局更改为getDomainLayout
. 但是当我使用代码时出现错误:
$this->layout = "getDomainLayout";
$this->layout = "getDomainLayout";
Yii2 throws:
Yii2 抛出:
Invalid Parameter – yii\base\InvalidParamException
The view file does not exist: \myyii2\modules\subDomain\views\layouts\bersih.php
回答by Oleksandr Pyrohov
There are several options to address this issue.
有几个选项可以解决这个问题。
Create a layout file in the appropriate module directory
在相应的模块目录中创建布局文件
An example below shows a canonical directory structure of some subDomain
module, including it's layouts (domain.php):
下面的示例显示了一些subDomain
模块的规范目录结构,包括它的布局 (domain.php):
subDomain/
Module.php the module class file
controllers/ containing controller class files
HomeController.php the home controller class file
models/ containing model class files
views/ containing controller view and layout files
layouts/ containing layout view files
domain.php the domain layout file
home/ containing view files for HomeController
index.php the index view file
Following this simple structure, you can set any layout by its name within the module's controller:
按照这个简单的结构,您可以在模块的控制器中按名称设置任何布局:
namespace myApp\modules\subDomain\controllers;
class HomeController extends Controller {
public function actionGetDomain() {
$this->layout = 'domain'; // equals 'myApp/modules/subDomain/views/layouts/domain'
}
}
This is the most preferable way, because modules are self-contained software units that consist of its own models, layouts, controllers, etc.
这是最可取的方式,因为模块是自包含的软件单元,由其自己的模型、布局、控制器等组成。
Specify the complete path to the directory that contains your layout file
指定包含布局文件的目录的完整路径
In some cases you might want to use a layout file that is located outside the module directory:
在某些情况下,您可能希望使用位于模块目录之外的布局文件:
class HomeController extends Controller {
public function actionGetDomain() {
$this->layout = '@app/views/layouts/main';
}
}
Where @appis the base path of the currently running application, for example:
其中@app是当前运行的应用程序的基本路径,例如:
myApp/frontend
In this situation, be sure that the main.phplayout file exists in the following directory:
在这种情况下,请确保main.php布局文件存在于以下目录中:
myApp/frontend/views/layouts/main.php
回答by zhisme
If I need different layout in controller, I simple add the following code
如果我需要在控制器中使用不同的布局,我只需添加以下代码
public function beforeAction($action)
{
$this->layout = 'layout'; //your layout name
return parent::beforeAction($action);
}
be sure that your layout is exists in appropriate folder
确保您的布局存在于适当的文件夹中
'@app/views/layouts/layout.php'
回答by rafaelvaloto
You can set the variable in the controller.
您可以在控制器中设置变量。
class DefaultController extends Controller
{
public $layout = 'main.php';
}
Or by passing the complete path
或者通过传递完整路径
public $layout = '@frontend/modules/idModule/views/layouts/main.php';
回答by Russell Sk.
Another one convenient way of using different layouts in your application is to create an abstract class. For example:
在应用程序中使用不同布局的另一种方便的方法是创建一个抽象类。例如:
abstract class AdminBaseController extends Controller
{
public function beforeAction($action)
{
$this->layout = '@app/views/admin/layouts/main.php';
return parent::beforeAction($action);
}
...
}
And then just extends
your controller.
然后只是extends
你的控制器。
class ArticlesController extends AdminBaseController { ... }
回答by Muhammad Omer Aslam
If you want to set it by default inside the module you can do that by writing
如果您想在模块内默认设置它,您可以通过编写
$this->layout = '@frontend/modules/user/views/layouts/main';
inside the init()
function of your module class your complete code will look like following
在init()
您的模块类的函数中,您的完整代码如下所示
public function init() {
parent::init();
$this->layout = '@frontend/modules/user/views/layouts/main';
// custom initialization code goes here
}
回答by Opad
You can add $this->layout = 'main'; in the module init method. The main.php should be located inside the modules view folder under layouts.
您可以添加 $this->layout = 'main'; 在模块 init 方法中。main.php 应该位于布局下的模块视图文件夹内。
回答by SaidbakR
Also you can set the layout
property in construtor
like the following:
您也可以像下面这样设置layout
属性construtor
:
class ArticlesController extends Controller
{
public function __construct($id, $module, $config = array()) {
parent::__construct($id, $module, $config);
$this->layout='main4articles';
}
....