Initial setup of a Kohana project, revisited.

Difficulty: Easy

Hi again folks,

The last article dealt with setting up a single web application, but what if you need to set up multiple applications? For example I need to set up a site that is comprised of an administration application, a private section for our sales department and a public site.

Well its actually very easy! Using the structure defined in the original article copy and paste the applications directory for each web app your going to create, giving each a unique and meaningful name, e.g. app_sales, app_admin, etc. You should end up with something like the following:

  • app_admin // administration application
  • app_sales // sales application
  • app_public // public website
  • modules
  • system
  • webroot

In your webroot directory create folders to correspond to the newly created applications. I called these folders sales and admin. In these copy/paste index.php and edit it to reflect the locations of the application, modules and system folders. e.g. $config['system'] = “../../system”; Your original index.php file in the webroot should point to where your public website resides.

Right, now simply open the config file for each application (application/config/config.php) and edit the site domain to point to your newly created folders. i.e.

app_admin/config/config.php: $config['site_domain'] = ‘localhost/admin/’;

app_sales/config/config.php: $config['site_domain'] = ‘localhost/sales/’;

app_public/config/config.php: $config['site_domain'] = ‘localhost/’;

Thats it, you now have multiple applications set up. These share the same system and modules.

The End……?

Not quite. Since all of my applications get their data from the same source, i.e. database, they all would be using the same models. Its unpractical to have a copy of each model for each application (goes against the DRY principle) so I needed a means of sharing them, I need a module!!!

So I created a module to share my models between the applications. Its very simple, create a folder in the modules directory and call it rubber_duckies (or a more meaningful name if u wish :) ). In this folder create a new directory called models. Now place any model that will be needed by the other applications in here.

  • modules / rubber_duckies / models

Finally back to the config file for each application and load your newly created module by adding it to the modules array:

$config['modules'] = array( MODPATH . ”rubber_duckies’ , MODPATH . ‘forge’, ….. );

Thats it, you have multiple applications all sharing the same models! Go code some magic :)


10 Responses to “Initial setup of a Kohana project, revisited.”

  1. marlus araujo Says:

    great tutorial, but one question: how we could write de .htaccess file so we van access these 3 applications keeping index.php invisible?

    Thanks in advance!

  2. bennythemink Says:

    good question Marlus. i’m afraid i am not an .htaccess expert so i cannot help you here. i suggest asking it in the forums as there are a few ppl there that know .htaccess configuration very well.

  3. Cyland Says:

    when using this method session is not working. it keep regenerate the new session id. how can we fix this.
    I’m running on winxp, with Apache/2.2.6 (Win32) PHP/5.2.5.

  4. oliverHR Says:

    in most configurations apache htaccess affect all subdirectories by Inheritance.

    root/.htacces
    root/code/affected
    root/app/(affected)
    root/—/sub/(affected)

    You can write rules or exceptions.

    Read this:
    http://corz.org/serv/tricks/htaccess2.php#section-inheritance

  5. piarte008 Says:

    Anybody found a solution to .htaccess issue?

    Please, do post it here. I’m pulling my hair out. Spent 2 days “hacking” my .htaccess file but can’t find any working solution.

    Thank you!

  6. jarek_bolo Says:

    Shouldn’t setting RewriteBase to appropriate values be enough?
    For public site RewriteBase /
    For admin RewriteBase /admin/
    For sales RewriteBase /sales/

    I have that structure in one site and it is working like that.

    What is more annoying in this solution the config files, upload locations, etc.

    Someone was also complaining on Session regeneration, check your config files carefully especially path setting for cookies.
    I didn’t have any problems with Sessions.

  7. jez Says:

    Where is this first tutorial to which you refer ??? I cant find a link to it anywhere!! (although you say i should refer to it).

  8. Jake Says:

    Here is the first tutorial (I needed it too)

    http://learn.kohanaphp.com/2008/07/24/initial-setup-of-a-kohana-project/

  9. hyeongjin.yong Says:

    # Turn on URL rewriting
    RewriteEngine On

    # Installation directory
    RewriteBase /

    # Protect application and system files from being viewed
    RewriteRule ^(application|modules|system) – [F,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(admin|sales|public)/(.*) $1/index.php/$2 [PT,L]

    # Allow any files or directories that exist to be displayed directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* index.php/$0 [PT,L]

  10. college pee cam Says:

    nice! i’m gonna make my own blog