Get base URL in Yii console application - php

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;

Related

Yii2 UrlManager config

I have a UrlRoute as below:
<?= Url::toRoute(['/site/show-post', 'id' => $model->id, 'type_id' => $model->type_id]) ?>
which give the result as
mysitename.com/site/show-post/1405?type_id=1
I want to add the post title to url and I modified the above as
<?= Url::toRoute(['/site/show-post', 'id' => $model->id, 'type_id' => $model->type_id,'title'=>$mdoel->title]) ?>
which results in:
mysitename.com/site/show-post/1405?type_id=1&title=your+post+title
So it is working fine. but I want to modify it like & it should work getting the same result.
mysitename.com/site/show-post/1405/type_id/1/your+post+title
even here 'id'=>$model->id is returning as I expect that is /show-post/1405 but the subsequent parameter is resulting in query-string
I also tried in config.php under - UrlManager like this:
'<controller:[A-Za-z-]+>/<id:\d+>/<type_id:\d+>/<title>' => '<controller>/<action>',
but it doesn't seems to work.
How I can achieve it like what I mentioned.
How about:
'<controller:[A-Za-z-]+>/<action:\w+>/<id:\d+>/<type_id:\d+>/<title>' => '<controller>/<action>',
The method you are following is correct and the results you are getting are the expected result. I am not sure what you meant by query language, but what understand is you want the clean urls instead of extra characters being added to your urls. For this you will have to implement pretty urls for yii2.
Here is how you can get pretty urls in yii2, add the following code to your configs:
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],

Yii2 URL Route for make a Slug Url

I'm learning on yii2,
I have a problem when make a url route.
here's my url configuration
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
// 'enableStrictParsing' => false,
'enablePrettyUrl' => true,
'rules' => array(
'category/<id:\S+>' => 'category/detail',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'request-password-reset' => 'site/request-password-reset',
'reset-password/<token:\S+>' => 'site/reset-password',
'profile/<id:[0-9a-zA-Z\-]+>/?' => 'profile/user',
'logout' => 'site/logout',
'login' => 'site/login',
),
],
The problem is in profile routing.
Here's the case:
http://localhost/myapps/profile/some-url/ -> work with added slash in
the end of url
http://localhost/myapps/profile/some-url -> without slash in end of url isn't working and error 404
http://localhost/myapps/profile/someurl -> not working error 404.
http://localhost/myapps/profile/first-second-third -> with our without slash in the end is working
I'm really confuse, I've been looking for previous question here and try out nothing working with my case.
Can somebody help with my problem?
Just move your Profile alias to the top of the list:
'profile/<id:[0-9a-zA-Z\-]+>/?' => 'profile/user',
'category/<id:\S+>' => 'category/detail',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'request-password-reset' => 'site/request-password-reset',
'reset-password/<token:\S+>' => 'site/reset-password',
'logout' => 'site/logout',
'login' => 'site/login',
As '<controller:\w+>/<action:\w+>' => '<controller>/<action>' was defined before 'profile/<id:[0-9a-zA-Z\-]+>/?' => 'profile/user', it will never reach profile, because it recognized it as a controller/action.

how to config yii2 urlManager rules with aliases and $_GET parameter

for my current (advanced) yii2-based project i just need one controller (SiteController). So there is no need to show it in the url. Thats why i added this rule to the frontend config:
'urlManager' => [
'rules' => array(
'<alias:product|contact|about>' => 'site/<alias>',
),
This is working fine and localhost/product points to localhost/site/product.
Of course, i activated prettyUrl and added this default rules to the common config:
'rules' => array(
'<controller:\w+>/<id:\w+>' => '<controller>',
'<controller:\w+>/<action:\w+>/<id:\w+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
Now i want to access a GET parameter like this: localhost/product/productname. But i get the error:
Unable to resolve the request "product"
but localhost/site/product/productname is working properly...
The "productname" should be $_GET['id']. What do i have to change to make this happen?
Thanks!
Your rules should be before the default ones, and you need 2 rules, e.g. :
'rules' => array(
'<alias:contact|about>' => 'site/<alias>',
'<alias:product>/<id:\w+>' => 'site/<alias>',
'<controller:\w+>/<id:\w+>' => '<controller>',
'<controller:\w+>/<action:\w+>/<id:\w+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
Just define var in your action, for example public function actionProduct($id) and $id been is $_GET['id']

Controller can't find the view in Yii

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);

Yii's URL manager rules reading from a MySQL table

Is it possible to have this kind of array:
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'urlFormat' => 'path',
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'/admin' => '/admin/home',
'/admin/<controller:\w+>' => '/admin/<controller>',
'/admin/<controller:\w+>/<action:\w+>/<id:\d+>' => '/admin/<controller>/<action>',
'/admin/<controller:\w+>/<action:\w+>' => '/admin/<controller>/<action>',
),
),
Be read from a database table?
Why: I'm making a CMS in which I want to define a custom categories, pages and posts base so that I don't use a base controller.
Example: My Pages controller is PagesController and I can make a rule in URL manager like this:
/pages/some-page-alias
I want to be able to change /pages/, /categories/ and /posts/ to something localized, meaning to be able to change it i.e. on Bosnian:
/kategorije/ => /categories/,
/stranice/ => /pages/,
/clanci/ => /posts/,
Is there a solution for this, how it can be done?
Btw, I have not tried anything because I have no idea...
You can simply do this:
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'urlFormat' => 'path',
'rules' => array(
'kategorije/<id:\d+>' => 'category/view',
'kategorije/<action:\w+>/<id:\d+>' => 'category/<action>',
'kategorije/<action:\w+>' => 'category/<action>',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'/admin' => '/admin/home',
'/admin/<controller:\w+>' => '/admin/<controller>',
'/admin/<controller:\w+>/<action:\w+>/<id:\d+>' => '/admin/<controller>/<action>',
'/admin/<controller:\w+>/<action:\w+>' => '/admin/<controller>/<action>',
),
),
URLManager prioritizes by what is listed first. So if you put in a custom rule above the standard rules it will run those rules first. Once it finds a rule that applies it breaks out.
As for me, your question is not clear.
If you want to store array in DB you can do it by serialize'ing it before.
If you want multiple names pointing to same controller you can use such a rule:
array(
'<_c:(stranica|page)>/<id:\d+>'=>'myController/view',
)
In this case if any of urls
http://example.com/stranica/123
http://example.com/page/123
is requested, it will point to
http://example.com/myController/view?_c=stranica&id=123
http://example.com/myController/view?_c=page&id=123
accordingly. See CUrlManager examples in API.

Categories