Yii2 app with CraftCMS (built on YII1.1)

Hey guys/gals,

I’m beginning the process of breaking our PHP app that is built inside CraftCMS out into a Yii2 app. Before I get into my question(s) here are some background details:

Tech stack:

CraftCMS 2.6.2993 (uses Yii 1.1.19)

AngularJS (1.6)

PHP 7.0.x

Ubuntu 16.04 (Apache 2.4.18)

How it works:

1.) User visits a URL and Craft determines that’s a URL we have defined (non 404).

2.) CraftCMS serves up the HTML via a template which we plug our angular app/partials into.

3.) User interacts with something on the screen (ie: submit button) that requires us to go get data from an external source (another REST api).

4.) JS function called which runs an $http.post() to the backend (same server so just posting to index.php).

5.) CraftCMS controller receives request and processes then returns JSON back to the AngularJS app.

What I need to do:

Run Yii along side CraftCMS on the same server and have them differentiate which one should be receiving given request.

Move template loading into YII (that’s phase 2).

The end goal is to have CraftCMS run as “headless/content as a service” where it’s essentially just a content management API to request content from and serve up on the page. Yii will then be handling all of the template loading and PHP business logic instead of having them as CraftCMS plugins.

Technical details:

Currently the way we call CraftCMS to get session data/etc is to do an $http.post() to /index.php and the postbody contains an "action" key that specifies which counter should receive this request.

Unfortunately I’m having trouble calling a Yii controller using the same logic. We have a “validate” plugin that contains a controller “Validate_AddressController.php” with a function “actionValidateAddress()”. This action receives a JSON body with the address and it then calls out to an external vendor to actually validate the address.

However, when I try to post to this controller action I’m unable to hit it. It appears that CraftCMS is receiving the request first and then returning a 404 immediately and/or YII is never receiving the request.

Folder Structure:


<project folder>

    <craft>

    <backend>

        <yii files>

            <controllers>

                 - AddressController.php (the file I want to call).

    <src>

        <js files>

How can I set this up to differentiate which requests should go to CraftCMS and which ones should go to Yii2? Am I attempting to post incorrectly to Yii? I’ve tried to do $http.post(’/address/validateAddress’, {body}) but this doesn’t seem to work.

TL;DR – I want to treat Yii2 almost like a restful API but it needs to exist on the same server as another YII app.

i am guessing you want your craft app to still function so I suggest you prefix your yii2 routes and detect them with your server nginx/apache can do pretty good url rewriting. in your application change the urls to point to yii2 app.

So I want CraftCMS to serve up my homepage located at / and the about page located at /about and so on. If I want yii2 to servce up /start I could prefix it with /yii2/start and run a Rewrite condition for anything with /yii2/ to be directed over to yii and rewrite it to /start?

I understand the htaccess rewrite conditions there, but how would I actually direct those requests with /yii2/ over to yii2? That might depend on how my server/app are structured I guess but that part is confusing me.

if you land in your yii2 directory you hit index.php you can pretty much put anything in your routes in config file and have it point to your controller. I can help you to get you off the ground on skype I am alirz23 on skype.