i use below code for url manager in Yii2 Framework :
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'<module:\w+>/<action:\w+>' => '<module>/default/<action>'
]
],
but. when enable the rule that remove default from url for modules , the rule for controllers stops working.
where the problem is my code?
thanks.
The problem is - your module rule overriders controller one.
The <module> and <controller> are not a keywords for UrlManager, so - in your case it does not works.
Your Url-config should be like that:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'<module:\w+>/<controller:\w+><action:\w+>' => '<module>/default/<action>'
]
],
It will use the default controller any time. Do you really want it?
It is better to use just '<module:\w+>/<controller:\w+><action:\w+>' => '<module>/<controller>/<action>'
Related
I have yii installed on my live server we have two interfaces one backend and one frontend. In backend we add products and in front end the view is rendered for the products. The problem is that for example this is our website https://mmstore.be/products/714/IPhone-12-Mini-Scherm-Reparatie product page if you put anything at the last of the url like this https://mmstore.be/products/714/IPhone-12-Mini-Scherm-Reparatie/03003300303094949949494 the page will remain the same means it should be showing not found page but the same page comes again.
Below is my config file code frontend.
<?php
$params = array_merge(
require __DIR__ . '/../../common/config/params.php',
require __DIR__ . '/../../common/config/params-local.php',
require __DIR__ . '/params.php',
require __DIR__ . '/params-local.php'
);
return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'frontend\controllers',
'components' => [
'request' => [
'csrfParam' => '_csrf-frontend',
],
'cache' => [
'class' => 'yii\caching\FileCache',],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
],
'session' => [
// this is the name of the session cookie used for login on the frontend
'name' => 'advanced-frontend',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'baseUrl' => '',
'enablePrettyUrl' => false,
'showScriptName' => false,
'enableStrictParsing' => true,
'rules'=>array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'class' => 'yii\web\UrlNormalizer',
'collapseSlashes' => true,
'normalizeTrailingSlash' => true,
),
],
],
'params' => $params,
];
i have error.php in my site directory and below is its code
<?php
/* #var $this yii\web\View */
/* #var $name string */
/* #var $message string */
/* #var $exception Exception */
use yii\helpers\Html;
$this->title = $name;
?>
<h1><?= Html::encode($this->title) ?></h1>
<div class="alert alert-danger">
<?= nl2br(Html::encode($message)) ?>
</div>
<p>
The above error occurred while the Web server was processing your request.
</p>
<p>
Please contact us if you think this is a server error. Thank you.
</p>
It is strange that you disabled "enablePrettyUrl", and using pretty url. If "/products/" is a controller name and assuming "IPhone-12-Mini-Scherm-Reparatie" is a parameter named "url" then you can change urlManager like this:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'/'=>'site/index',
'/products/<id>/<url>'=>'products/view',
'/products/<id>'=>'products/view',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
in controller it should be something like this
public function actionView($id, $url) {
$product = Product::find()->where(['id'=>$id])->one();
// ...
}
I've been working on a project and the first thing I did was adding the Authentication and Authorization plugins. I did not used AuthComponent at all since its deprecated.
Now I want to add extra logic on the database and installed the ACL Plugin. I haven't managed to find documentation and all the examples on the internet implement the plugin using the old AuthComponent.
What I need is find a way to make for the ACL to use the new Authentication plugin
This is my AppController.php:
public function initialize(): void
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
// Add this line to check authentication result and lock your site
$this->loadComponent('Authentication.Authentication');
$this->loadComponent('Authorization.Authorization');
$this->loadComponent('Acl', [
'className' => 'Acl.Acl'
]);
$this->loadComponent('Auth', [
'authorize' => [
'Acl.Actions' => [
'actionPath' => 'controllers/',
'userModel' => 'Users'
]
],
'authenticate' => [
'Form' => [
'fields' => ['username' => 'email'],
'userModel' => 'Users'
],
],
'loginAction' => [
'plugin' => false,
'controller' => 'Users',
'action' => 'login'
],
'loginRedirect' => [
'plugin' => null,
'controller' => 'Users',
'action' => 'index'
],
'logoutRedirect' => [
'plugin' => null,
'controller' => 'Users',
'action' => 'login'
],
'unauthorizedRedirect' => [
'controller' => null,
'action' => 'login',
'prefix' => false
],
'authError' => 'You are not authorized to access that location.',
'flash' => [
'element' => 'error'
]
]);
/*
Found on stackoverflow that "These two plugins are not ment to work together, cakephp/acl is strictly ment for use with the deprecated auth component. If you want ACLs for cakephp/authorization, then you need to implement that yourself".
I have no idea how to do that, so Im still listening if anyone has any idea on how to do that or what could be another solution.
I want to try to make an URL from backend to frontend, I tried to add the following code in backend/config/main.php but it didn't worked.
'urlManagerFrontEnd' => [
'class' => 'yii\web\urlManager',
'baseUrl' => '/',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
Then when I tried this code, it showed "localhost/admin" instead just "localhost" (where the frontend is).
echo Html::a(Yii::t('app', 'test'), [Yii::$app->urlManagerFrontEnd->createUrl(['/'])]);
Just for reference, both frontend/web and backend/web are inside a folder called "public". frontend/web is in the root of public folder while backend/web is inside an admin folder which is inside the public one.
An example of implementation on a project with three subdomains.
Config in common
'urlManagerBackend' => \yii\helpers\ArrayHelper::merge(
[
'hostInfo' => env('BACKEND_HOST_INFO'),
'baseUrl' => env('BACKEND_BASE_URL'),
],
require(Yii::getAlias('#backend/config/_urlManager.php'))
),
'urlManagerFrontend' => \yii\helpers\ArrayHelper::merge(
[
'hostInfo' => env('FRONTEND_HOST_INFO'),
'baseUrl' => env('FRONTEND_BASE_URL'),
],
require(Yii::getAlias('#frontend/config/_urlManager.php'))
),
Env - it is configuration file
FRONTEND_HOST_INFO = http://site.lan
BACKEND_HOST_INFO = http://admin.site.lan
another project, realization - backend - web.php
$config = [
'components' => [
'urlManagerF' => [
'class' => 'yii\web\urlManager',
'baseUrl'=>env('FRONTEND_HOST_INFO'),
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [],
],
'urlManagerS' => [
'class' => 'yii\web\urlManager',
'baseUrl'=>env('STORAGE_HOST_INFO'),
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [],
],
],
],
i want to specify that i have almost no experience with php and yii framework
I am trying to setup a classified ads website with yii ,but after i did some seo audit i realised that almost all of my category pages have duplicates like this :
example.com/category?slug=cars -this url is a duplicate that i don't want
example.com/category/cars -this is the url that i want to have
This is my urlManager code :
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
'search' => 'site/search',
'contact' => 'site/contact',
'<controller:conversation>/<action:delete>' => '<controller>/<action>',
'<controller:conversation>/<action:reply>/<conversation_uid:[a-z0-9_\-]+>' => '<controller>/<action>',
'<controller:account>/<action:invoices>/<page:\d+>' => '<controller>/<action>',
'<controller:account>/<action:conversations>/<page:\d+>' => '<controller>/<action>',
'<controller:listing>/<action:index|update|package|preview>/<slug:[a-z0-9_\-]+>' => '<controller>/<action>',
'page/<slug:[a-z0-9_\-]+>' => 'pages/index',
'<controller:category>/<action:location|map-view|get-map-location>' => '<controller>/<action>',
[
'pattern' => 'category/<slug:[a-z0-9_\-]+>/<page:\d+>',
'route' => 'category/index',
],
[
'pattern' => 'category/<slug:[a-z0-9_\-]+>',
'route' => 'category/index',
],
[
'pattern' => 'category/map-view/<slug:[a-z0-9_\-]+>/<page:\d+>',
'route' => 'category/map-view',
],
[
'pattern' => 'category/map-view/<slug:[a-z0-9_\-]+>',
'route' => 'category/map-view',
],
[
'pattern' => 'store/<slug:[a-z0-9_\-]+>/<page:\d+>',
'route' => 'store/index',
],
[
'pattern' => 'store/<slug:[a-z0-9_\-]+>',
'route' => 'store/index',
],
'<url:.+/>' => 'site/redirect'
],
],
If you need any further detailes or specific code ask me and i will try to provide !
Thanks
how to change default language from en to another language
$config = [
'on beforeAction' => function ($event)
{
Yii::$app->language = 'fa';
},
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'language' => '/fa',
'components' => [
'jdate' => [
'class' => 'jDate\DateTime'
],
'mycomponent' => [
'class' => 'app\components\MyComponent',
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => '******',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'urlManager' => [
'class' => 'codemix\localeurls\UrlManager',
// Disable index.php
'languages' => ['fa', 'en'], // List all supported languages here
'showScriptName' => true,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
// '' => 'site/index/fa',
// '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
// '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => require(__DIR__ . '/db.php'),
/*
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
*/
],
'params' => $params,
];
this is my $config in web.php
i want set default to 'fa' in root site !
my site automatic change lang to 'en' in first time in any browser!
i try to change lang with
'on beforeAction' => function ($event)
{
Yii::$app->language = 'fa';
},
but that is dosent correct work!
Remove this ridiculous 'on beforeAction' thing and just set
'language' => 'fa', // NOT '/fa'!
سلام
ویدئو های آقای صیف زاده رو مشاهده کردین؟
لینک زیر آموزش های ویدئویی ایشون هست . در شماره های 17 و 18 و 19
این موضوع رو آموزش دادن.
https://drive.google.com/drive/folders/0B4ZlNlar4Ij6XzJrbVZOejRCcGM