It has long been the task to collect two projects into one. There was a shop on the ShopScript 5 platform and a small application on Yii. It was necessary to make a general authorization, personal account and, in general, to combine these two projects into one.
Finally, they reached out and it was decided to hook up the Yii to ShopScript as a separate application. The application will be called Tracker.
About what has been done - in order:
1. Create an empty ShopScript application
Creating a blank application is quite well described in the manual on the site
ShopScript .
')
In principle, nothing supernatural. The application will be just two actions:
- BackendAction - in which we describe the DefaultAction. We shall leave it empty for the time being;
- FrontendAction - in which we describe the execute method that implements all the frontend logic. We will also leave it empty for now.
For the application, it is also necessary to create a config file, several icons and views. This is all described in detail in the manual on the ShopScript website, so it makes no sense to describe it here.
2. Create an application on Yii
So nothing new.
We put the application on Yii in the apptrackers folder, which will be in the root of the site.
Either we drop an already finished application into the folder, or we create a new one right there. No difference. The bottom line is that in the apptrackers folder there is our application on Yii.
How to install the application can also be found on the
official website .
It was a preparation. Then the fun begins.
3. Run the Yii application via ShopScript
In order to fully transfer control to Yii in the ShopScript application, we configure the administrative panel on the “Structure” tab to call our application at
trackers / * , and in the application we will leave one FrontendAction, which will always be called when you call an address like
your_site / trackers * .
In the FrontendAction itself, in the execute method, which implements the logic of the project, we write one line:
require_once($_SERVER["DOCUMENT_ROOT"]."/apptrackers/index.php");
This means that if we enter an address like
your_site / trackers * in the address bar, ShopScript will send it to our application for execution. And since Since the application describes only one default action, it will always be called.
As a result, we received a call from our Yii application. It remains to configure our Yii application.
4. Configure Yii
Now, knowing that our application is already running, it remains to register the necessary configs.
- Set up our UrlManager so that it displays the address in the form of a path, and does not show the script being executed. We also need urles to be beautiful and of the same type.
- Next - we indicate that the url in our application will begin with trackers:
'request' => array( 'baseUrl' => '/trackers', ),
- Since Our Yii application is in a folder that no one should know about; we will move the styles and assets into a separate folder in the root of the site. Let's call it tracks . Inside we will create at once the assets folder. There we will put all our style files, js-scripts and other vermin.
- Now we will configure AssetsManager, specifying to it where our files will lie:
'assetManager' => array( 'basePath' => dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'tracks'.DIRECTORY_SEPARATOR.'assets', 'baseUrl' => '../../tracks/assets', ),
- Also, we will slightly correct the basic layout by specifying our folder in the included scripts:
<link rel="stylesheet" type="text/css" href="/tracks/css/screen.css" media="screen, projection" /> <link rel="stylesheet" type="text/css" href="/tracks/css/print.css" media="print" /> <link rel="stylesheet" type="text/css" href="/tracks/css/main.css" /> <link rel="stylesheet" type="text/css" href="/tracks/css/form.css" />
Now, when calling our application at
your_site / trackers - we will see our application Yii.
Fine! We made it! But going to gii there was one nuance.
Gii publishes all of its resources to assets by default in the application folder. We need to redefine this path.
To do this, specify the path to resources in the gii config:
'gii'=>array( 'class'=>'system.gii.GiiModule', 'password'=>'******', 'assetsUrl'=>'/tracks/assets/gii',
Copy all the files for gii from the module / apptrackers / framework / gii / assets / * directory to the folder for our files / tracks / assets / gii / * and get a full-featured project.
You could certainly have made everything easier. Simply place the yii application in a separate directory where yii will be called. Without any additional settings. But the indisputable plus of the solution described above is that in the Yii application we have all the functionality of ShopScript with already ready authorization, user management, shopping cart and so on and so forth ...
I will show it on the example of the user module.
5. Add user module to Yii
In fact, we already have users in ShopScript, we have their basket and some of the functionality that we want to leave.
We proceed as follows:
Create a modules folder in Yii and in it a directory for our user module.
Add the module file UserModule.php, in which we leave the standard functionality for initialization and add the static isAdmin method:
public static function isAdmin() { wa()->getUser()->isAdmin(); }
From the code it is clear that we are accessing an already existing ShopScript application, we obtain from it a user and administrator status.
We’ll also add a class to the components folder that will be responsible for the operation of SSUser.php users, in which we will describe several standard methods:
class SSUser extends CWebUser { public function getId() { return wa()->getUser()->getId(); } public function isAdmin() { return wa()->getUser()->isAdmin(); } public function getIsGuest() { if(wa()->getUser()->getId()) { return false; } return true; } public function getName() { return wa()->getUser()->get('name'); } }
From the code it is clear that we take all the data for the user from an already running ShopScript application.
Let's tweak in the config class, which is responsible for users:
'user'=>array( 'class' => 'SSUser', ),
Now, the standard code in layouts / main.php, where the user check is:
$this->widget('zii.widgets.CMenu',array( 'items'=>array( array('label'=>'Home', 'url'=>array('/site/index')), array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')), array('label'=>'Contact', 'url'=>array('/site/contact')), array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest), array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest) ),
in the last two paragraphs, it will display the link depending on whether the user is authorized in the ShopScript application or not, and if authorized, will output the user name from the same application.
In the future, the module can be expanded to the required functionality.
This method of symbiosis of two applications, of course, does not pretend to be ideal, but for solving many problems, I think it should be appropriate.