Yii2 + AngularJS Yii2 + AngularJS angularjs angularjs

Yii2 + AngularJS


htaccess file looks correct but if you have your webserver's DocumentRoot and your webapp in separate folders, you will need to start by setting the RewriteBase path in your htaccess file somthing like this if using a unix based OS:

RewriteEngine onRewriteBase /~userName/pathToYourYiiProject/web# If a directory or a file exists, use the request directlyRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d# Otherwise forward the request to index.phpRewriteRule . index.php

if you don't get results when acceding to the path manually from your navigator then you will need to check if pretty urls are activated first :

'urlManager' => [                'class' => 'yii\web\UrlManager',                'enablePrettyUrl' => true,                'showScriptName' => false,            ],

(check Mihai's link for yii official documentation)

check also if rewrite_module is activated in your apache configs. to enable and load the mod_rewrite in a linux OS you will have to launch those scripts :

$ cat /etc/apache2/mods-available/rewrite.load$ sudo a2enmod rewrite$ ls -al /etc/apache2/mods-enabled/rewrite.load

finally remember that recent Apache versions (from 2.3.9) have "AllowOverride None" by default, you will have to change it to "AllowOverride All" in /etc/apache2/apache2.conf (again path in linux OS dont know if its the same in other OS). restart your apache server & recheck manually the path in your navigator, if you get json format results, then your back-end is working fine & angularJs should be able to communicate with it.


The problem is that your WebServer must serve the AngularJS index view for all paths that begin with /construct/. Here's a solution for a URL Rule in Yii2 that does just that:

In your config/main.php

'components' => [    // other components here    'urlManager' => [        // other URL Manager settings here        'rules' => [            'construct/<path:(.*)>'=>'construct/index',        ],    ],]

In your controllers/ConstructController.php:

public function actionIndex($path = '/'){    // serve your angularJS index page here}


Because you are working on the virtual directory (e.g. Alias /site/ "/dir/"), you must use

RewriteBase /site/in your apache configuration file.