I'm just start working with Yii2.
------------- Problem number 1: load view error ----------------- I have a Site module with SiteController and it's views.
actionIndex(){
$this->render('index');
}
It show error: the view ROOT_PATH/views/site/index.php not found (note: ROOT_PATH is driectory in my local).
In init() function in the ROOT_PATH\\modules\\Site\\Module.php file: i added statement to test: echo $this->getViewPath();
Result: ROOT_PATH\\modules\\Site\\views
I want to ask : why it do not load index.php in /modules/Site/views folder ??? (It load index.php in ROOT_PATH\\views folder so it not found that view file)
------------- Problem number 2: Load action error --------------
When i add to ROOT_PATH\\modules\\Site\\Module.php file: Yii::$app->setLayoutPath($this->getLayoutPath());
Now it can load correct view for actionIndex. But links this view do not working:
$menuItems = [
['label' => 'About', 'url' => ['/site/about']]
]
Html is generaated on Front-End:
<a href="/site/about">About</a>
Why href is '/site/about' , i want this value will be "site/site/about"
to call 'about' link you have to create about.php in views/site and create a function which name must be actionAbout() in sitecontroller
in sitecontroller
public function actionAboute()
{
$this->render('about');
}
in index.php
<?= Html::a('About', ['value'=>Url::to('index.php?r=site/about')]) ?>
and for load view problem check your config file and cookieValidationKey once.
All Modules search for the view templates in their module path. So if you have a module named "site" and inside the module a controller named "SiteController" it searches the view in the folder
ROOT_PATH\\modules\\site\\views\\site\\
ROOT_PATH\\modules\\[modulename]\\views\\[controlername]
If you directly have the controllers in your application in \\controllers the viewpath will be
ROOT_PATH\\modules\\views\\site\\
ROOT_PATH\\modules\\views\\[controlername]
a Link in the view which starts with / always starts from the root. in your case you just need ['about']
to link to the same controller (and same module).
if you link to a controller in a another module you also have to give the module name which is also in your case site
.
['/site/site/about']
would also link to your site controller in your site module. But as i mentioned above if you want to link to the same controller in the same module it isn't needed.
You can find the guide about routing and URL creation here:
http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html
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.