The process of connecting Yii 1.1.15 to the CMS as an application using the example of Shop-Script 5

The task has long been to collect two projects into one. There was a shop on the ShopScript 5 platform and a small application on Yii. It was required to make a general authorization, a personal account and, in general, combine these two projects into one.
Finally, we got around and it was decided to hook Yii to ShopScript as a separate application. The app will be called Tracker.

About what has been done - in order:

1. Create an empty ShopScript application


Creating an application blank is pretty well described in the manual on the ShopScript website .

In principle, nothing supernatural. The application will consist of only two actions:
  • BackendAction - in which we describe DefaultAction. We will leave it empty for now;
  • 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 a view. This is all described in detail in the manual on the ShopScript website, so it does not make 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 located in the root of the site.
Either drop the finished application into the folder or create a new one right there. No difference. The bottom line is that our app on Yii is in the apptrackers folder.
How to install the application can also be found on the official website .

It was a preparation. Then the fun begins.

3. Launch the Yii application through ShopScript


In order to completely transfer control of Yii in the ShopScript application, we set up the call of our application at the trackers / * address in the administrative panel on the “Structure” tab , and in the application we’ll leave one FrontendAction, which will always be called when the address like yoursite / trackers * is called .
In FrontendAction itself, in the execute method, which implements the project logic, we write one line:

        require_once($_SERVER["DOCUMENT_ROOT"]."/apptrackers/index.php");

This means that if we enter the address of the form yoursite / trackers * in the address bar , ShopScript will send it to our application for execution. And since only one action is described in the application by default, then it will always be called.

As a result, we got a call to our Yii application. It remains to configure our Yii application.

4. Configuring Yii


Now, knowing that our application is already running, it remains to register the necessary configs.

  1. We will configure our UrlManager so that it displays the address in the form of path, and does not show the executable script. We need urls to be beautiful and of the same type.
  2. Next, we indicate that the url in our application will start with trackers:

            'request' => array(
                'baseUrl' => '/trackers',
            ),
    

  3. Because our Yii application is in a folder that no one should know about, we will take out the styles and assets in a separate folder in the root of the site. Call it tracks . Inside, we will immediately create the assets folder . There we will add all our style files, js-scripts and other evil spirits.
  4. Now configure AssetsManager, telling 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',
            ),
    

  5. Also, we’ll tweak the main layout a bit, specifying in our plug-in scripts just our folder:




Now, when calling our application at yoursite/trackers , we will see our Yii application.
Excellent! We made it! But going into gii there was one caveat.
Gii publishes all its resources to assets by default in the application folder. We need to redefine this path.
To do this, specify the path to the resources in the gii config:

		'gii'=>array(
			'class'=>'system.gii.GiiModule',
			'password'=>'******',
                        'assetsUrl'=>'/tracks/assets/gii',
			// If removed, Gii defaults to localhost only. Edit carefully to taste.
			'ipFilters'=>array('xxx.xxx.xxx.xxx','::1'),
		),


We copy all the files for gii from the directory of the module / apptrackers / framework / gii / assets / * to the folder for our files / tracks / assets / gii / * and we get a fully-functional project.

Of course, everything could be made easier. Just put 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 ShopScript functionality with ready-made authorization, user management, shopping cart, etc., etc., etc. I will

demonstrate this using the example of the user module.

5. Adding a user module to Yii


In fact, we already have users in ShopScript, there is their basket and part of the functionality that I want to leave.
We proceed as follows:

Create the modules folder in Yii and the directory for our user module in it.
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();
	}


The code shows that we are referring to an existing ShopScript application, we get the user and administrator status from it.

We also add a class to the components folder that will be responsible for the work 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');
    }
}


The code shows that we take all the data for the user from the already running ShopScript application.

We’ll tweak the class in the config that 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 a link depending on whether the user is authorized in the ShopScript application or not, and if authorized, it will display the user name from the same application.

In the future, the module can be expanded to the necessary functionality.

This method of symbiosis of two applications, of course, does not claim to be ideal, but for solving many problems, I think it should come up.

Also popular now: