Yii: .htaccess and urlManager for separate backend and interfaces

I find it difficult to configure my .htaccess and urlManager in a Yii project to have an interface in http://www.example.com and a backend in http://www.example.com/backend with the following folder structure. Any help is appreciated. Thanks.

/assets /backend /controllers /config main.php /models /views /common /models /protected /controllers /config main.php /models /views .htaccess backend.php index.php 

Solution: after a lot of help @ bool.dev all that it works, so I add here every necessary final file. In the interface, I use the path format for url and hide index.php

/backend/config/main.php

 $backend=dirname(dirname(__FILE__)); Yii::setPathOfAlias('backend', $backend); return array( 'basePath' => $backend, 'controllerPath' => $backend.'/controllers', 'viewPath' => $backend.'/views', 'runtimePath' => $backend.'/runtime', ...); 

/protected/config/main.php

 'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName'=>false, 'rules'=>array( '<controller:\w+>/<id:\d+>'=>'<controller>/view', '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>', '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', ), ), 

.htaccess

 Options +FollowSymLinks IndexIgnore */* <IfModule mod_rewrite.c> RewriteEngine on RewriteBase /yii/example/ RewriteRule backend backend\.php [T=application/x-httpd-php] # if a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # otherwise forward it to index.php RewriteRule . index.php </IfModule> 

backend.php

 $yii=dirname(__FILE__).'/../../yii/framework/yii.php'; $config=dirname(__FILE__).'/backend/config/main.php'; require_once($yii); Yii::setPathOfAlias('common', dirname(__FILE__).DIRECTORY_SEPARATOR.'common'); Yii::createWebApplication($config)->run(); 

index.php

 $yii=dirname(__FILE__).'/../../yii/framework/yii.php'; $config=dirname(__FILE__).'/protected/config/main.php'; require_once($yii); Yii::setPathOfAlias('common', dirname(__FILE__).DIRECTORY_SEPARATOR.'common'); Yii::createWebApplication($config)->run(); 
+6
source share
1 answer

According to this article on the Qiang wiki , you can make the following changes and work:

 // backend.php: require('path/to/yii.php'); Yii::createWebApplication('backend/config/main.php')->run(); 

Then in your base configuration (e.g. backend / config / main.php):

 $backend=dirname(dirname(__FILE__)); $frontend=dirname($backend); Yii::setPathOfAlias('backend', $backend); return array( 'basePath' => $backend, 'controllerPath' => $backend.'/controllers', 'viewPath' => $backend.'/views', 'runtimePath' => $backend.'/runtime', 'import' => array( 'backend.models.*', ), // ... other configurations ... ); 

But for this we need the main .htaccess to direct example.com/backend to backend.php, which I still do not understand.

Edit:
Just realized:

 RewriteEngine On RewriteBase /projectroot/ RewriteRule backend backend\.php [T=application/x-httpd-php] 

RewriteBase was important to me, since backend.php was not found when I did not give the correct project, basically it should be the directory where you have the backend.php script entry.

+6
source

Source: https://habr.com/ru/post/916072/


All Articles