Controller can't find the view in Yii - php

I want a view from another module to be rendered and I use path '//modulename/foldername/viewname'(without .php), but when I run it I get "Controller can't find the view" error. What do I do wrong? It used to work before. My version of Yii is 1.1.15.
EDIT:
These is my urlManager:
'urlManager' => array(
'class' => 'yupe\components\urlManager\LangUrlManager',
'languageInPath' => true,
'langParam' => 'language',
'urlFormat' => 'path',
'showScriptName' => false,
'cacheID' => 'cache',
'rules' => array(
)
But how can it be related to my problem? Doesn't urlManager has nothing to do with view aliases?

give it the full path to that file:
$this->render('application.modules.modulename.foldername.viewname' , $arrayData);

Related

Add language to url in Yii2

I have following url for example:
http://mywebsite.com/testing/test
And I want it to be something like this depending on the current language:
http://mywebsite.com/en/testing/test
I have language stored in: Yii::$app->language
I tried to modify my URLManager (/config/web.php), but it doesn't work for me.
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => [
'/login' => 'site/login',
'/logout' => 'site/logout',
Yii::$app->language.'/'.'<controller:\w+>s'=>'<controller>/index',
Yii::$app->language.'/'.'<controller:\w+>/<id:\d+>/<title:\w+>'=>'<controller>/view',
Yii::$app->language.'/'.'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
Yii::$app->language.'/'.'<controller:\w+>/<action:\w+>/<id:\d+>/*'=>'<controller>/<action>',
],
],
Can You help me?
I found an extension. Even if you don't want to use it, you can look into it’s code to get an idea how it works. (Changing patterns / routing / etc.)
https://github.com/codemix/yii2-localeurls

urlmanager with modules not working in yii2.0

This regarding module creation inside backend folder.Example i have created a module name as "api". And also sccessfully created controller for that modules.
here is my urlmanager code:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'rules' => [
'<module:\w+>/<controller:\w+>/<action:\w+>'=>'<module>/<controller>/<action>',
'<module:\w+><controller:\w+>/<action:update|delete>/<id:\d+>' => '<module>/<controller>/<action>',
],
]
when i access url with respect to module as "api" , controller as "country" and action as "create"
http://local2host.com/bootstrap/backend/web/index.php/api/country/create
it showing 404 Not Found error
Where i am going wrong?
The second rule will never work, Also you do not have to define the rules and the normal ones will work. So this
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
will make this link /website.com/core/contact/index work just fine.
But I am not sure why your route is not catching the first rule... strange. It should.
Better late then never.
For make module works you need to append it in config:
'modules' => [
......
'modulename' => [
'class' => 'app\modules\modulename\Module',
],
......
],

How to set the default controller in yii2

I have UserControler and I run it in virtual server http://basic.com/index.php?r=user/index. How can I set up UserController and action index to be the default when I go to http://basic.com
Did you try in your config:
'defaultRoute' => 'user/index'
Default Controller
Like few people already said, you need to add defaultRoute in the configurations file.
Here is how it should look:
//config/web.php in basic template or backend/config/main.php in advanced
$config = [
...
'components' => [
...
],
'params' => $params,
'defaultRoute' => 'user/index',
];
This can be set within the config, see Default Controller:
[
'defaultRoute' => 'main',
]
But note that this is closely related to routing, which can be completely customized by urlManager component. Then if you want let's say domain/profile to behave like domain/user/profile then these rules for urlManager might be another way to go:
'rules' => array(
'<action:\w+>' => 'user/<action>', // <-- use UserController by default
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
Hopefully this will help someone :)
Open the web.php file from your configuration folder, and add the following:
'defaultRoute' => 'admin'
to your $config array.
Note that defaultRoute is ignored, when strictParsing is set to enabled in the urlManager config. Refer to the issue on GitHub: https://github.com/yiisoft/yii2/issues/5892
The following config is recommended as a measure:
[
...
'defaultRoute' => 'default/index',
...
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
...
'rules' => [
...
'' => '', // <- this line should be added
],
],
...
]
This is not the answer of question, however it might be helpful to know:
there is a catchAll property in yii\web\Application which is defined as:
The configuration specifying a controller action which should handle all user requests.
Usage:
'catchAll' => ['controller/action']
So, every request to http://basic.com will call controller/action
With yii2 I was able to do it in the urlmanager with:
'rules'=> [
['pattern'=>'<action>', 'route'=>'controller/<action>'],
]
Try the other solutions... If they do not work, then use my simple trick...
Just create an index.php page at the root.
Then, in that file, write this code:
return header('Location: http://your page location');
If 'defaultRoute' don't work - check settings for 'urlManager'. Maybe default route is set there.
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'' => 'site/index', // this line should be chenged to ''=>''.
'<controller:\w+>/<action:\w+>/' => '<controller>/<action>',
],
],

Yii: DataProvider pagination is not following url rules defined in config

I'm listing users on my website with pagination of 15 users per page. It works just fine and I've re write the url like this.
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'caseSensitive' => false,
'rules' => array(
'people' => 'Users/index',
...
---
As you can see I re route users/index to people. When I create a url it just also works fine but this isn't working in dataprovider pagination as I'm using CListView to render users list. It creates url in the pagination like this: users/index/users_pagination/2 how to solve problem?
you may like this,
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'caseSensitive' => false,
'rules' => array(
'people/*' => 'users/index',
---

Get base URL in Yii console application

How to get base URL in a Yii CConsoleApplication application?
I tried Yii::app()->request->getBaseUrl(true) and ended up with the following error.
Undefined index: SERVER_NAME (/var/www/yii/framework/web/CHttpRequest.php:279)
There is no request object in a console application. the request object in a web application its an instance of CHttpRequest, if you are generating URLs in an offline task, you have to configure the baseUrl in some other way, perhaps in the configuration:
"request" => array(
'hostInfo' => 'http://localhost',
'baseUrl' => '/yii-project/index-test.php',
),
// OR
'request' => array(
'hostInfo' => 'http://localhost',
'baseUrl' => '/yii-project',
'scriptUrl' => 'index-test.php',
),
For Yii2 advanced template, create params.php file if it doesn't exist in config directory of your console or common app and paste the following code:
return [
'frontendUrl' => 'http://yourdomain.com'
];
So that it can be accessed in the following way in console:
echo Yii::$app->params['frontendUrl'];
You have to add in config/console.php in components array
'request' => array(
'hostInfo' => 'http://localhost',
'baseUrl' => '',
'scriptUrl' => '',
),
and also add urlManager so it will remove index.php?r=site/action from the url and you will have pretty urls
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'<controller:\w+>' => '<controller>/index',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
),
Please try the following wayt to get the base url page,
echo Yii::app()->getBaseUrl(true);
get the home url like this:
http://www.demo.local
echo Yii::app()->homeUrl;

Categories