Sharing config among multiple applications in codeigniter - php

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');

Related

CakePHP cannot find database configuration from shell in plugin

Background: Basically, my application has a plugin which has a Shell within it. This Shell is located in the plugin's Console > Command folder. Functions from this shell are intended to be ran from the command line.
My database configuration file (database.php) is located in the application's root Config folder. I access the database in several other instances so I know there's nothing wrong with the database configuration.
When I try to use any Model data retrieval methods (such as find(...)), I get the error:
The datasource configuration "default" was not found in database.php
This is occurring because it's looking for the database.php file inside the Plugin's Config folder, rather than the root Config folder. This only occurs when using a Shell, rather than in a Controller.
My Question: How can I tell my Plugin to use the root's database.php file instead of trying to find it in it's own Config folder, when using a Shell?
To reiterate: The Plugin uses the root Config/database.php file when retrieving data from a model in a Controller, but tries to use its own Config/database.php file when retrieving data from a model in a Shell.
Two things that I've tried that will work: 1) Moving database.php into the Plugin's Config folder and 2) Creating a symbolic link.
Both solutions seem unacceptable as I don't want to have two copies of my database.php file around, and creating a symbolic link isn't a great solution since this application will likely have to be distributing among different systems.
Thanks. I should mention that this is Cakephp 2.3.6
Answer (thanks user221931): Your working path needs to be the application path, not the plugin path. You can run the plugin shell using the plugin dot notation. Like this:
cake -app /path/to/app Plugin.shellName shellFunction
as opposed to how I was incorrectly doing it before:
cake -app /path/to/app/Plugin/pluginName shellName shellFunction
How about running the plugin shell using -app?
Changing Paths:
Your working path should be the same as your application path. To change your path use the '-app' param.
Example: -app relative/path/to/myapp or -app /absolute/path/to/myapp

Yii Share models between two applications

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();

config codeigniter to run first project

I am using xampp and my document root in apache is set to htdocs directory by default. I download Codeigniter and unzip the project into this directory. It runs fine. Now I would like to use CI to create my own project. In the controllers folder I would like to create a folder named “myproject” and in the views folder of application folder, I would like to create a folder named “myproject_view” in which I will store all of my view files. My problem is I don’t know how to reset my config file (especially the route.php) for my project to work then.
CodeIgniter documentation doesn’t have a section to specify how to do this, the information given in URI Routing chapter is not enough for readers to understand this at all. Plus, What if I also would like to store my controler files in a nested folder (controllers > somefolder > someanotherfolders > etc) ? Thank you very much.
[UPDATE]
If you only leave all controller in controllers folder, and view files in the views folder, then things work out easily, what if you create a folder in teh controllers and a folder for view in the views folder. I guess you need to change your default route configuration. I would like to know HOW ?
CodeIgniter's documentation covers Sub-Folders for Controllers.
Views on the other hand, are always loaded from controllers. Therefore, you can write:
$this->load->view('my_folder_name/my_view');
Routes are not required here.

What is the most scalable PHP-based directory structure for a large site?

I am creating a very large PHP MVC-based site that will have a large library of php classes, javascripts, and many css files (not to mention a large amount of files for the MVC).
For the first time ever, I am actually taking the time to plan out a clean and organized directory structure.
What directory structures do you typically use, and which will be easiest to manuever when there are thousands of files?
This is my setup. It's worked great for me for small - very large projects (including a social network).
These folders would all live within my main application folder:
config - contains custom PHP config files
css - contains the project's CSS files
helpers - contains 'helper' files (each file is a collection of functions)
images - contains the project's images
js - contains the project's Javascript files
lib - contains PHP classes specific to the project
modules - My MVC framework allows packaging site sections as modules
blog - An example module
controllers - contains the controllers for the module
models - contains the models for the module
views - contains the views for the module
views - contains views that should be globally accessible (page header, footer, etc)
All the directories could obviously contain sub-folders that would further organize your files. For example, the 'css' folder could have sub-folders named 'web' and 'mobile'. The 'images' folder could contain a 'user_uploaded' folder which could then contain`'profile'. And of course you can add folders as you see fit, in one project I have a folder called 'uploaders' which just contains stand-alone upload scripts.
I also use convenience methods which help construct the filenames of what I want to load. For example, my loadView() will look for the view file in the current module directory, or if you pass an optional $module argument, it will look specifically within that module's folder.
I hope this helps.
You should have one directory as web root, where only files you want exposed to the whole internet should reside.
project/
web/
index.php
css/
js/
images/
config/
lib/
web/ is the root shown to visitors
lib/ is here the library folder, and where autoload look for files.
You can add more subfolders to project/ like controller, modules, view, helper, etc. This depends on your framework.
EDIT:
If you use composer (which I recommend) and maybe npm with grunt and less your file structure would be the following:
project/
web/
js/
css/
images/
index.php
cli/
config/
config.php
node_modules/
src/
test/
vendor/
composer.json
composer.lock
packages.json
web/ has all your public files
cli/ scripts and programs to be run from command line NOT the web
config/ has all your config files (in git you ignore config.php and instead have config.dist.php without usernames, passwords, validation codes and table prefixes/suffixes and other "secrets")
node_modules/ has all your library files from npm (in git I suggest you put this in a submodule)
src has all your local PHP files in psr4 structure, set up to autoload in composer.json
test/ has all your unit tests for your src classes, set up in autload-dev in composer.json (remember to use composer install --no-dev on live, maybe add -o if you don't have too many classes)
vendor has all your library files from composer and the ONE AND ONLY autoload.php to be included in web/index.php and any cli scripts (in git I suggest you ignore this vendor folder)
Add other folders and files as required for your project.
For deployment use this structure:
/sites/project/ (project is your projectname)
current (alias to current release folder releases/v1.1.0)
previous (optional alias to previous release folder releases/v1.0.1)
releases/
v1.0.0/ (git checkout of tag v1.0.0)
v1.0.1/ (git checkout of tag v1.0.1)
v1.1.0/ (git checkout of tag v1.1.0)
shared/ (has all your shared files and folders to be aliased in all releases - maybe something like GlusterFS)
Make a deployment script. Something like this:
First take backup of db or to copy it to a new database, checkout git repo to new folder with release tag, get all git submodules, run composer install --no-dev, setup any aliases for shared folders and files like uploaded images and configuration files, generate js/css with grunt and less or equivalent, point current alias to the new folder with the tag, run update database script, restart nginx/apache/fpm-php services, run tests to check the website is up.
Have a script to go back to previous version (or a guide so you know what to do).
For core files which are included:
approot/inc/
For data access functions and classes are in:
approot/dao/
For javascripts:
approot/scripts/
For CSS:
approot/styles/
For images:
approot/img/
For static content (normally for user profile pictures or uploaded images):
approot/static/
For caches:
approot/caches/
For templates or View files:
approot/templates/
All pages file:
approot/
Structure from Samstyle PHP Framework
The answer I posted here was from 2009. Over the years more standards were published, including PSR-0 which covers the topic on folder structure. I also have a new (and I feel that it's better) folder structure with Packfire Framework.
In my experience, you can never plan for this. You can try to follow what frameworks do, but I find I never quite fit exactly into their mold.
I recommend to just keep a good rule of thumb for 20 files in a directory maximum. If you find you need more, just create a few sub directories and move common components in there.
This is mostly a matter of preference, a quick Google search would reveal many different project structures. But it would be really nice if there were an agreed upon standard. I think this initiative by the PHP Package Development Standards is a good candidate.
This is the directory structure they propose:
bin/: command-line executables
config/: configuration files
docs/: documentation files
public/: web server files
resources/: other resource files
src/: PHP source code
tests/: test code
EDIT:
This is also mentioned in the PHP The Right Way under the section Common Directory structure.
I use codeigniter for small and big projects.
It's MVC feature is moderately good.
codeIgniter\system\application\config : contain all kind of configuration files like DB,Payment gateway, ftp config, routes and ...
codeIgniter\system\application\models: contain all kinds of database classes, you should create sub folders according to your need, I used customers, mailData, paymentModel, report, web-service and ....
codeIgniter\system\application\views: contain all kinds of files that will work as output for clients, you should think of reuse these files if possible. Like the models you had to create sub folder like administration, reports, email, email_template .....
codeIgniter\system\application\controllers : this is the most important part. This will help to create SEO url, so you should be more careful about sub folders this time. You can create like administration, products, reports, orders..... and consider a good name for the functions of the controller class.
These were for the PHP/HTML file.
Now about the other files:
codeIgniter\images: for the images
codeIgniter\scripts: for the Java scripts and their framework
codeIgniter\styles: for the CSS
codeIgniter\uploads: for the uploaded files, if you don't want to put files in the DB
For the detail see codeIgniter framework in detail.
Here "codeIgniter\" is the approot
This is the structure i'm using currently,
public/
assets/ /* js, css, imgs, ... */
index.php
src/
config/ /* for config files */
helpers/ /* for functions */
libraries/ /* for free classes that are not MVC classes */
models/ /* for M in MVC */
views/ /* for V in MVC */
controllers/ /* for C in MVC */
vendor/ /* for vendors files */
uploads/ /* for uploaded images, docs, ... */
Have a look at symfony 1.4 or symfony 2 dir structure. Choose what's most intuitive to you.
I believe this depends on how large the project will become. This is what I used mostly:
project/
index.php
img/
css/
js/
views/
functions/
As long as all the project files are organised...
Even though the question is abit old, I still think it is wise to suggest the latest scaleable application structure which I have been working in my SOA based application and working absolutely fine.
myApplication/
app/
config/
+ this can include custom MVC structure
cli/
docker/
lib/ - most commonly reusable components
logs/
public/ - should contain all publicly exposable web contains
sql/ - db migration stuffs
tests/ - compulsory test
tools/ - application addon tools like any kinds of rulset etc
vendor/

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