How to config yii2 urlManager rules? - yii2

Here is Yii2 code in main.php:
'urlManager' => [
'baseUrl' => $baseUrl,
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'suffix' => '.html',
'rules' => [
// site controller
'' => 'site/index',
'contact_us' => 'site/contact',
// sitemap controller
'sitemap' => 'sitemap/index'
]
And url in browser is:
+ Site controller:
- http://localhost/neko/
- http://localhost/neko/contact_us.html
+ Sitemap controller:
- http://localhost/neko/sitemap/index.html
How to I configure my sitemap controller to http://localhost/neko/sitemap.xml?

Use array configuration for sitemap.xml, like that:
'rules' => [
// site controller
'' => 'site/index',
'contact_us' => 'site/contact',
// sitemap controller
[
'pattern' => 'sitemap',
'route' => 'sitemap/index',
'suffix' => '.xml',
],
],
See docs.

Related

Yii2 advanced - enable pretty URL in module

I am learning how modules work in Yii2 and now I created the following module: gdpr. I can access the following route: /index.php?r=gdpr/user/index. However, I want to access the route like this: /gdpr/user/index. How can I achieve that?
config.php:
<?php
return [
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'modules\gdpr\default'],
['class' => 'yii\rest\UrlRule', 'controller' => 'modules\gdpr\user'],
],
],
],
'params' => [
// list of parameters
],
];
You need to configure controllers in this way:
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['gdpr/default', 'gdpr/user'],
],
],
],
],
See https://www.yiiframework.com/doc/api/2.0/yii-rest-urlrule#$controller-detail

RBAC Routes add default route of module to all the routes of project?

If I add this configuration of the cms module to the config file
'cms' => [
'class' => 'yii2mod\cms\Module',
'controllerNamespace' => 'backend\controllers',
'defaultRoute' => '',
'froalaEditorOptions' => [
'clientOptions' => [
'heightMin' => 300,
'theme' => 'dark',
'imageUploadURL' => 'upload-image',
'imageManagerDeleteURL' => 'delete-image',
'imageManagerDeleteMethod' => 'POST',
'imageManagerLoadURL' => 'images'
],
'excludedPlugins' => [
'file',
'emoticons'
]
],
'enableMarkdown' => false
]
It adds the default route of this module to all the routes like this
/cms/site/login /cms/site/index /cms/site/error. Why this is happening and how i can remove this?
If you want to remove the /cms module prefix by default, you can add a global route to backend/config/main.php(If you use advanced templates): '<controller:[\w-]+>/<action:[\w-]+>' =>'cms/<controller>/<action>'.
for example:
// backend/config/main.php
return [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:[\w-]+>/<action:[\w-]+>' =>'cms/<controller>/<action>'
],
],
];
Access in your bowser: www.xxx.com/site/index, it is forwarded to: /cms/site/index

can't configure urls from backend to frontend Yii2

Web frontend - localhost (directory apache2/htdocs in the LAMP), web backend - localhost:8080/backend (apache2/htdocs/backend). Yii advanced application in the same directory with htdocs - mafia-yii (apache2/mafia-yii). File main.php in backend/config:
'components' => [
....
'urlManager' => [
// here is your normal backend url manager config
'class' => 'yii\web\UrlManager',
'baseUrl' => 'http://localhost/backend',
],
'urlManagerFrontend' => [
'class' => 'yii\web\UrlManager',
'hostInfo' => 'http://localhost',
'baseUrl' => 'http://localhost',
],
],
file ~/lampstack-7.0.4-0/apache2/mafia-yii/backend/views/layouts/main.php :
....
$menuItems[] = ['label' => 'Backend', 'url' => ['/site/index']];
$menuItems[] = ['label' => 'Fronend', 'url' => [Yii::$app->urlManagerFrontend->createUrl('/site/index')]];
....
Result:
http://localhost:8080/backend/index.php?r=backend%2Findex.php%3Fr%3Dsite%252Findex
Not Found (#404)
Have you tried with:
'components' => [
....
'urlManager' => [
// here is your normal backend url manager config
'class' => 'yii\web\UrlManager',
'baseUrl' => '/backend',
],
'urlManagerFrontend' => [
'class' => 'yii\web\UrlManager',
'hostInfo' => 'http://localhost',
'baseUrl' => '',
],
],
Try to use the following code:
use yii\helpers\Url;
$menuItems[] = ['label' => 'Fronend', 'url' => Url::to(Yii::getAlias('#web') . '/site/index', true)];
I solve the problem. Because:
$var[] = Yii::getAlias('#webroot');
$var[] = Yii::getAlias('#web');
\yii\helpers\VarDumper::dump($var);
returns:
[ 0 => '/home/kira/lampstack-7.0.4-0/apache2/htdocs/backend' 1 => '/backend' ]
the solution is:
$menuItems[] = ['label' => 'Frontend', 'url' => '#web/../'];

Get Parameters Not returning correctly when using desired urlformat

I am having a bit of trouble with get params in Yii2. I have code like the following:
Url::to(['support/about', 'id' => 100]);
And this returns the below:
/support/about.php?id=100
Which is exactly what I was after. But when I then try to reverse engineer this by entering that into the address bar and trying to get the value of id using the below:
echo Yii::$app->request->getQueryParam('id');
echo Yii::$app->request->get('id');
echo $_GET['id'];
I get nothing at all.
I do however get the correct value when I use:
/support/about/100.php
My url manager is like below:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'suffix' => '.php',
//'cache' => 'cache',
//'scriptUrl' => '',
//'baseUrl' => '/',
//'hostInfo' => 'http://www.yourhost.com.au',
'routeParam' => 'r',
'ruleConfig' => [
'class' => 'yii\web\UrlRule'
],
'rules' => array(
[
'pattern' => '',
'route' => 'site/index',
'suffix' => '',
],
[
'pattern' => '<action:\w+>',
'route' => 'site/<action>',
'suffix' => '.php',
],
[
'pattern' => '<controller:support>',
'route' => '<controller>/index',
'suffix' => '/',
],
[
'pattern' => '<controller:support>/<action:\w+>',
'route' => '<controller>/<action>',
'suffix' => '.php',
],
[
'pattern' => '<module:\w+>/<action:\w+>',
'route' => '<module>/default/<action>',
'suffix' => '.html',
],
[
'pattern' => 'gii',
'route' => 'gii',
'suffix' => '',
],
[
'pattern' => '/<controller:\w+>/<action:\w+>',
'route' => 'gii/<controller>/<action>',
'suffix' => '',
],
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
Let's narrow down question a bit.
Since you have issue with particular URL we can remove stuff that doesn't matter for the case from URL manager config:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
[
'pattern' => '<controller:support>',
'route' => '<controller>/index',
'suffix' => '/',
],
[
'pattern' => '<controller:support>/<action:\w+>',
'route' => '<controller>/<action>',
'suffix' => '.php',
],
],
],
I've removed values that are the same by default, rules that never apply and global prefix since you're setting it per
rule anyway.
Now test controller:
<?php
namespace app\controllers;
use yii\web\Controller;
class SupportController extends Controller
{
public function actionIndex()
{
return 'Hello, I am index.';
}
public function actionAbout()
{
echo \yii\helpers\Html::a(\yii\helpers\Url::to(['support/about', 'id' => 10]), ['support/about', 'id' => 10]);
echo '<br>';
echo \yii\helpers\VarDumper::dump(\Yii::$app->request->get(), 10, true);
}
}
Make sure that you webserver rewrite rules are correct for .php that aren't index.php. To verify that you may
change all suffixes to .html. I did since my server is configured to serve .php directly.
When following http://example.com/support/about.html?id=10 I'm getting the following:
/support/about.html?id=10
[
'id' => '10'
]
That means Yii works fine and the problem is about webserver config.

YII2 Redirect to backend after user registration from frontend

After installation of advance template in yii2, I got a user registration from at the frontend but I want it to redirect to backend after registration. How can that be done???
public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome(); // I WANT TO CHANGE THIS TO REDIRECT TO LOCALHOST/MYAPP/BACKEND/WEB
}
}
}
return $this->render('signup', [
'model' => $model,
]);
}
UPDATE
here is the urlmanager
'urlManager' => [
'class' => 'yii\web\urlManager',
'showScriptName' => false,
],
'urlManagerBackend' => [
'class' => 'yii\web\urlManager',
'showScriptName' => false,
'baseUrl' => 'http://localhost/ncddp/backend/web/index.php',
],
You can configure separate urlManager component in frontend for backend:
'urlManager' => [
'class' => 'yii\web\urlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
'urlManagerBackend' => [
'class' => 'yii\web\urlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'baseUrl' => 'http://admin.site.com',
],
Put in components section in application config.
Then you can use it like that:
Yii::$app->urlManagerBackend->createUrl(...);
Usage with redirect:
return $this->redirect(Yii::$app->urlManagerBackend->createUrl(...));
Related links:
Issue on Github
Url Manager component
Routing and URL Creation