Yii Share models between two applications - php

I've two web applications which are located on one server but on different domains (for example app A is administration and app B is client).
The problem is that I want to share models (ActiveRecords) from application A to be available in application B.
Is there any clever way to do this?
Thanks!

Try to make an alias in one app to the second ( And from the 2nd to the 1st ;) ) using
YiiBase::setPathOfAlias()
Documentation:
http://www.yiiframework.com/doc/api/1.1/YiiBase#setPathOfAlias-detail

Sure, just follow a few easy steps:
1. Put your models in a shared directory
For example, if your current directory structure looks like this:
/www
/application1
/protected
/models
/application2
/protected
/models
Create another "shared" directory. It's a good idea to put some structure in there as well, in case you want to share more than just some models:
/www
/application1
/protected
/models
/application2
/protected
/models
/shared
/models
Put the active record models you want to share in /www/shared/models.
2. Alias the shared directory in both applications
Go to your main.php configuration file in both applications and create an alias for the shared directory:
Yii::setPathOfAlias('shared','../shared/'); // or use an absolute path
3. Import shared models
Still in your main.php configuration, import your shared models:
'import'=>array(
// ...existing imports here...
'shared.models.*',
),
You can now directly refer to the shared classes anywhere in your application and Yii will load the appropriate classes automatically.
If you later add more directories to /shared then simply add corresponding lines to the import configuration.

Yii::setPathOfAlias('applicationA','path/to/applicationA/protected');
Then when you making import in config:
'import' => array('applicationA.models.*'....
Now you will be able to use models from appA in appB.
Same can be done with modules, controllers and views.
Views - viewPath
Modules - modulePath
Controllers - in index.php add
$app->setControllerPath('////protected/controllers');
Before $app->run();

Related

Symfony3 routing not working with web root configuration

I am trying to integrate some new Symfony3 apps into an existing web space. At my webroot /html, each app has it's own directory. Each of these apps could be anything- cakePHP, custom PHP, whatever. And each are accessed by a URL like localhost/appname. This structure is not flexible and I am not able to add anymore URL patterns to vhosts or anything like that.
Therefore, I have my Symfony3 install at /symfony which is a sibling directory of /html. Inside of /html I have a landing directory for my Symfony app: /html/symfonyapp.
In /html/symfonyapp/index.php I have one sole line of code:
require_once DIR.'/../../symfony/web/app.php';
In my Symfony set up, I have a bundle called SymfonyappBundle. I have a route configured in src/SymfonyappBundle/Resources/config/routing.yml to redirect calls to /symfonyapp to this particular Bundle.
The routing is not working. Calls to http://localhost/symfonyapp always end up going to the routing for "/" Why? I feel that it has nothing to do with my Symfony setup, but instead something to do with the request coming in through that /html/Symfonyapp/index.php file.
Any help is greatly appreciated!
Edit: I see it's helpful to list out the directory structure so here it is:
- /var/www/html <-- this is your (global) web root
|- cake-app
|- custom-php-app
|-symfonyapp
|—index.php (which contains only a require for app.php)
-/var/www/symfony <—symphony standard install here
|- app/
|- vendor/
|- src/
|- web/ <-- the web root for your symfony-app
|- .htaccess
|- app.php <-- the "boot"-script similar to index.php
I hope I got this right. Your directory structure looks something like this:
- /var/www/html <-- this is your (global) web root
|- cake-app
|- custom-php-app
|- symfony <-- the project root for you symfony-app
|- app/
|- vendor/
|- src/
|- web/ <-- the web root for your symfony-app
|- .htaccess
|- app.php <-- the "boot"-script similar to index.php
So basically you have a typical Symfony-application, but it sits inside a shared web root. So when accessing it you don't go to http://example.com/, but instead to http://example.com/symfony/web/
This might be the first problem you are having. Your application must be accessed from the web folder, not from the symfony-folder. Depending on for example some rewrite rules in /var/www/html/.htaccess you might not be able to look through files in the symfony-subfolder and there is no entry script, so it will not work. Dependening on your setup you might not even have permission to rewrite the url per .htaccess or in your server's config, this would complicate things a bit further. For now let's assume the .htaccess-file in web/ does work and it's just a matter of the wrong folder your url is pointing at.
There are multiple options you have if you want the url to be accessible at http://example.com/symfony/ (without the web/-part). Symfony's project structure is actually pretty flexible and you could get rid of the symfony/web/-folder and instead use symfony/ as your web root. There might be some gotchas for example with some install scripts that copy resources like css and js into your web-root. You could also run into issues when bundles point to the web-directory, e.g. for storing uploads. You probably have to make a few adjustments but a basic setup should be doable in no time by moving all files from web to the parent folder (including the .htaccess which might be hidden).
Another option might be to create a new .htaccess in symfony's project root that points to web/app.php instead of just app.php. You could take the existing file as a reference. I try to avoid using htaccess-files and don't have a setup right now were I could try it, but it might be worth a shot before moving lots of files around. Although you still might run into issues with assets where the path is not matched correctly.
edit: another option that's probably more work, but might be useful if you want to migrate away from the other existing web apps to just a Symfony app is, to move symfony to the same level as html/ and move all the stuff from web/ into html/. Now your server's web root is also symfony's web root (again you might have to fiddle around with assets expecting to be in a folder called web/). Now you just need to make sure that whenever Symfony does not find a route it will pass the request to your other apps. There are several things to look out for and it's a lot more work than the 2 approaches above, but in can be useful. There was a pretty good talk about it at last year's SymfonyCon in Berlin on how to do this if you are interested in this route:
https://github.com/SymfonyCon/2016-talks#modernizing-with-symfony
https://slidr.io/derrabus/modernizing-with-symfony
Unfortunately the video of the talk is not out yet.
The solution that I found was to add a custom Kernel for each web application. So for each folder I have under my webroot /var/www/html, I added a new Kernel in Symfony. Each Kernel has it's own routing and config files, which solved my routing issues! I followed this symfony doc to set up the kernels: http://symfony.com/doc/current/configuration/multiple_kernels.html
App1:
webroot: /var/www/html/app1/index.php has the typical app.php code in it which initializes App1 Kernel as such: $kernel = new App1Kernel('dev', true);
symfony details: in app/config I added one directory per Kernel , so this would be app/config/app1. I copied all config, routing, service files into this directory and reference the custom Bundle for the app.
App2:
webroot: /var/www/html/app2/index.php has the typical app.php code in it which initializes App1 Kernel as such: $kernel = new App2Kernel('dev', true);
symfony details: in app/config I added one directory per Kernel , so this would be app/config/app2. I copied all config, routing, service files into this directory and reference the custom Bundle for the app and changed any references to Bundles to the appropriate bundle.

Single Codeigniter Code Base for Multiple Domains

I'm looking for a clear explanation of how to use CodeIgniter for multiple domains. I want a single set of models and config files, with the ability to call different controllers depending on the domain the request is coming from. After doing some research, I am fairly certain the way I currently have this set up is not ideal, and I would like to fix it before I get too far into building. Here's the current set up:
I have three domains that I'm managing:
domainA.com
domainB.com
domainC.com
Each domain points to a different folder on my server:
/domains
/global
/models
/model1.php
/model2.php
/domainA.com <--- domainA.com points here
/application
/controllers
/domainAController1.php
/domainAController2.php
/assets
/system
/index.php
/domainB.com <--- domainB.com points here
/application
/controllers
/domainBController1.php
/domainBController2.php
/assets
/system
/index.php
/domainC.com <--- domainC.com points here
/application
/controllers
/domainCController1.php
/domainCController2.php
/assets
/system
/index.php
Then, in each controller, I add a package path in the constructor function so that the controllers have access to my global models:
$this->load->add_package_path(APPPATH.'../../global/');
This allows me to load models from the controllers as normal with something like:
$this->load->model('model1');
The folder hierarchy I'm showing above is not complete. Inside each of the domain folders, there is a full CI installation. So if I want to define constants or configuration parameters for the entire "app", I essentially have to do it three times. Also, the more I develop, the more I am coming across needs for more advanced features like base models and controllers and I have a feeling this "package path" solution I"m using now is not going to work.
This clearly is not the right way to go about it, and I was hoping someone could offer me a better way to implement this.
I am running CodeIgniter version 2.1.4.
***************************UPDATE***************************
Here's what I ended up doing to get this working. I would love someone's advice on if this is a good or bad idea, meaning, do you foresee any obvious issues using this method?
I used routes and controller subfolders to accomplish what I needed. I now have a single installation of CI, and within my controllers folder I have subfolders for domainA.com, domainB.com and domainC.com.
Then, in my routes.php file, I added this after the reserved routes:
switch($_SERVER['SERVER_NAME']){
case 'domainA.com':
$controllerSubDirectory = 'domainA.com';
break;
case 'domainB.com':
$controllerSubDirectory = 'domainB.com';
break;
case 'domainC.com':
$controllerSubDirectory = 'domainC.com';
break;
default:
$controllerSubDirectory = '';
break;
}
route['(.*)'] = "{$controllerSubDirectory}/$1";
In other words, I just take the incoming request and route it as if it were actually coming in with the appropriate subfolder as it's first URI segment.
Are there any issues with this I am not thinking of? Maybe some sort of security risks?
The short answer: take advantage of CodeIgniter "Third Party Packages" feature.
Your directory structure is not very well organized. It is not necessary to store multiple copies of the CodeIgniter System files, assuming of course that all applications are using the same system (version and no/same modifications to core files).
Check CodeIgniter Docs for more detail.
As mentioned above, you could take advantage of CI's packages to package your common code as a 3rd party package. Then you can just load models and other resources as you normally would, and CI will know how to find them on it's own.

Sharing config among multiple applications in codeigniter

I am trying to create multiple application under 1 codeigniter application, however some of them would be sharing same config such as database config.
-application
-config
-models
-project_1
-config
-controllers
-models
-project_2
-config
-controllers
is it possible to load the config in the first level of application folder, and look for the application level's folder if the config file doesn't exist? I know it is possible when I load configs manually by using add_package_path(), but autoload doesn't work.
Why not keep it simple:
<?php
// This app has no config. Use the shared one
require_once('/path/to/common/config.php');
You should use packages:
http://ellislab.com/codeigniter/user-guide/libraries/loader.html
I tend to create a package called 'share' within the web root, i.e. packages/share
Inside your packages folder, you can create a folder structure similar that of the core CI application config, helpers, models, views etc.
Say you want to only have one DB file, you can place the db config file inside packages/share/config and call this file using the following within your applications database file:
include_once(FCPATH . 'packages/share/config/database.php');

yii vendors for all sites

folders structure:
htdocs:
--includes
----yii
------framework
--------vendors
----------my_vendor1.php
----------my_vendor2.php
--site1
--site2
How to import my_vendor1.php and my_vendor2.php for site1, site2 ?
Assuming that they are inside the yii/framework folder as you show, you can easily import anything in the Yii framework directory using the system Path Alias, like so:
Yii::import('system.vendors.*');
Assuming both sites using the same Yii installation, you can now call my_vendor1.php in either.
You can actually add this to each site's configuration file as well:
'import'=>array(
'application.models.*', // your regular site-specific imports
'system.vendors.*', // your custom imports
)

PHP: Storing objects in a Symfony plug-in module's lib/ directory

I am building a Symfony project, and have created a new plug-in named sfUtilsPlugin. I currently have a directory structure that looks like this:
sfUtilsPlugin/
modules/
sfSearchLucene/
actions/
config/
lib/
templates/
Now, in the sfUtilsPlugin/modules/sfSearchLucene/lib directory, I have an object called sfLucene. The idea was that this object is accessible from the Symfony auto loading mechanism, so that it can be instantiated from anywhere within the application.
However, simply adding the sfLucene.class.php file to the sfUtilsPlugin/modules/sfSearchLucene/lib directory does not appear to add it to the autoloader.
Does anyone out there know why this might be happening? Perhaps it is just not possible to automatically use objects stored in this location inside Symfony.
Any advice is appreciated.
Because you are adding this class in lib subdirectory of module sfLucene, it will be autoloaded only if current module is sfLucene.
You have two options:
put this class somewhere into sfUtilsPlugin/lib directory;
require them every time you need it

Categories