I'm trying to use spanjeta/yii2-backup
For Project GM it works fine. here I'm not using prettyurl
For Projectamitopticals I'm getting 404 error.I'm using pretty url here.
I'm providing comparative codes below
GM - the routing -
[
'url' => Url::to(['/backup']),
'label' => 'Backup',
'icon' => 'glyphicon glyphicon-send',
'visible'=>Yii::$app->user->can('c_billing-person'),
],
Amitopticals - the routing
[
'url' => Url::to(['/backup']),
'label' => 'Backup',
'icon' => 'glyphicon glyphicon-download-alt',
],
Output screenshot in GM
Output screenshot of Amitopticals
Log of GM
Amitopticals Log
Please let me know how to resolve this.
I've sorted it out
Changed rules for pretty url -
Previous pretty url rule
'rules' => [
'<alias:>' => 'site/<alias>',
],
Corrected pretty url rule
'rules' => [
'<alias:\W+>' => 'site/<alias>',
],
Thanks everyone.
Related
I'm trying to create a simple rule url, and I can't get it to work.
I want the following rule:
mysite.com/[username]
to go to
mysite.com/kit/page?id=[username]
Is this possible?. Right now I only have one rule, but I need to keep that one as well
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'' => 'site/index',
],
],
If I understand correctly you are saying that you have an existing page mysite.com/kit/page?id=[username] which should be shown if you type in the URL mysite.com/[username] in the address bar, if yes then you can update the urlManager like below
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'/' => 'site/index',
'<id:\w+>' => 'kit/page'
]
]
Ideally, I would use mysite.com/kit/[username] to avoid conflicts with any other controller which somehow matches up with any username and use 'kit/<id:\w+>' => 'kit/page'.
Note : \w+ matches any word character (equal to [a-zA-Z0-9_]), so if your username can have any other character allowed you might have to update the pattern, for example to allow - you should change the rule to '<id:[\w\-]+>' => 'kit/page'
This app ru language start
'language'=>'uz',
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#frontend/messages',
'sourceLanguage' => 'uz',
'fileMap' => [
'main' => 'main.php',
'yii' => 'yii.php',
],
],
],
],
'urlManager' => [
'class' => 'codemix\localeurls\UrlManager',
'showScriptName' => false,
'enableLanguageDetection' => true,
'enablePrettyUrl' => true,
// 'defaultLanguage'=>'uz',
'enableDefaultLanguageUrlCode' => true,
'languages' => ['uz', 'ru','oz','en'],
I am set system langugae uz, source langugae uz and codemix url manager one method have defaultLangugae but its not working
this error picture
Never used it but looking at the source code it says you need to use the languages option as an array of language codes. More specific patterns should come first, e.g. 'en_us' before 'en'. This can also contain mapping of <url_value> => <language>, e.g. 'english' => 'en'.
ou need to change the option to 'languages' => ['en_us'],
You must setup language for app config.
UPD:
Setup config(frontend/config/main.php) to needed language for app. Then first time app will launch in this language:
'language' => 'en'.
If this not work check in incognito mode, you must clean old session saved cookies
In Yii I am using below code to generate a URL
Yii::$app->urlManager->createUrl('site/delete',array('id' => 100))
It generates the URL like below and parameter ID is missing in the URL
http://localhost/yii-basic/web/index.php?r=site%2Fdelete
In the config file, rules are as below
'urlManager' => [
'enablePrettyUrl' => false,
'showScriptName' => false,
'rules' => [
'post/<id:\d+>/<title:.*?>'=>'post/view',
'posts/<tag:.*?>'=>'post/index',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
],
],
According to the documentation, the correct syntax would be:
Yii::$app->urlManager->createUrl(array('site/delete', 'id' => 100));
I find it more convinient to use \yii\helpers\Url class and short array syntax:
Url::to(['site/delete', 'id' => 100]);
I have quite a few actions in my SiteController. I don't want site in the URL, so I have this in my config file:
'rules' => [
'/' => 'site/index',
'about' => 'site/about',
'contact' => 'site/contact',
'login' => 'site/login',
'logout' => 'site/logout',
'register' => 'site/register',
],
Is there a way to simplify all of this (excluding the / route) in to one rule?
You can use
'rules' => [
'<action:\w+>' => 'site/<action>',
],
This is the best way:
'<action:(about|contact|login|logout|register)>' => 'site/<action>',
The answer posted by Shringiraj Dewangan will match any request and route to site controller, whereas this will only match the specified requests.
I'm using yii2 and i want url to be this way:
example: backend.dev/ads/browse/city/london
My code is this(backend/config/main.php):
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<city:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<city:\d+>' =>
'<controller>/<action>'
],
],
And my link is:
echo Html::a('<h4>'.$cities[$i].'</h4>', ['/ads/browse',
'city'=>$cities[$i]], ['class'=>'btn buttonCity']);
But the output is this: backend.dev/ads/browse?city=london
I want ? to disappear, help me please.
\d+ means it expects number but you gave london.
Change
'<controller:\w+>/<action:\w+>/<city:\d+>' => '<controller>/<action>'
to
'<controller:\w+>/<action:\w+>/<city:\w+>' => '<controller>/<action>'
Thanks to #Bizely
I have to make these changes:
'<controller:\w+>/<action:\w+>/<city:\d+>' => '<controller>/<action>'
change to:
'<controller:\w+>/<action:\w+>/<city:\w+>' => '<controller>/<action>'
My carelessness!