Welcome Guest, Not a member yet? Register   Sign In
How to: backend and frontend on 1 CI installation
#1

[eluser]FinalFrag[/eluser]
The last few days I have been looking around a lot about how to get multiple applications working on 1 CodeIgniter installation. I found the solution on my own in the end. Some of you might be looking for this, so I thought I might share it.

The problem
I have a website that needs a frontend and a backend. When I go to www.example.com or www.example.com/some/more/segments/here, the website should redirect to the frontend. However, if I request www.example.com/admin or www.example.com/admin/any/segments it should access the admin application.

The solution
I have my CI installation out of the wwwroot in a folder called /ci and the folder in the frontend is called /mysite.

/mysite contains 2 folders: back and front. Both contain resources for each part (images, css, javascript, etc.). There are also 3 files in /mysite. First of all there is .htaccess
Code:
RewriteEngine On

# PHP settings
php_flag magic_quotes_gpc off

# Force www
RewriteCond %{HTTP_HOST} !^www\.(.*) [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/mysite/$1 [R=301,NC,L]

# Always use index.php
RewriteCond $1 !^(admin\.php|.*\..*)
RewriteRule ^admin(.*)$ admin.php/$1 [L]

RewriteCond $1 !^(index\.php|.*\..*)
RewriteRule ^(.*)$ index.php/$1 [L]

index.php
Code:
$system_folder = "../../ci";
$application_folder = "application/front";

admin.php
Code:
$system_folder = "../../ci";
$application_folder = "application/back";

That's it for the /mysite folder. Now for the /ci folder. By default the CI download contains a folder application. In the application folder are config, controllers, errors, helpers, etc. My application folder under /ci looks like this:
Code:
/application
/application/back/config
/application/back/controllers
/application/back/errors
/application/back/helpers
/application/back/...
/application/front/config
/application/front/controllers
/application/front/errors
/application/front/helpers
/application/front/...

2 files need to be changed still to make this all work. /application/back/config/Config.php
Code:
$config['base_url'] = "http://www.example.com/mysite/admin/";

And /application/front/config/Config.php
Code:
$config['base_url'] = "http://www.example.com/mysite/";

All done
That's it. When you go to www.example.com/mysite/admin you get the /application/back application. If you go to www.example.com/mysite (as long as the first parameter doesn't start with admin) you get the /application/front folder.

Hope you like this little guide, I got frustrated a lot getting this to work. I hope your lives will be a little easier with this post.
#2

[eluser]Jameson.[/eluser]
Why not just have all the admin controllers in "application/controllers/admin/" subfolder, admin views in "views/admin/" and resources in a common folder? (Models for both front and backend should of course be the same)
#3

[eluser]FinalFrag[/eluser]
That is indeed also an option. Although my goal was to comletely separate the backend from the frontend. Having an admin folder in controllers/ and views/ isn't "clean" enough for me.

You say models have to be shared, but I don't think so. In the front end models, there are a lot of functions that you don't need in the backend. So you have a lot of overhead loading all those functions.

If you want to share models (or other resources for that matter), your option is probably better but I like to keep things really tidy.
#4

[eluser]FinalFrag[/eluser]
Oh, I just saw I forgot to mention my Routes.php in /application/back/config. I altered it to get some more flexibility.

Code:
$route['default_controller'] = $route['([A-Za-z0-9_\-]*)'] = "home";
$route['([A-Za-z0-9_\-]*)/(:any)'] = "$2";

Now, this routes file won't need to be changed when I decide to rename the back/ folder to idontknow/
#5

[eluser]iamzozo[/eluser]
I also try to do something like this, it's good for me if everything keep on their folders (mvc), but i would like to know how to use it: i have /admin which load admin class. But inside admin, i want to create separeted sections like managing news. The admin area has a template, and in it, there is an area for content (example: news).
Admin/news works as "class/function", what i need is "news" works also as a controller:
admin/news/thing as class/class/function.

How can i solve this?
I found that i can also call CI functions inside the view, but i want to keep the original order (mvc).

Finally, i'm a designer not a programmer Smile
#6

[eluser]TheFuzzy0ne[/eluser]
Code:
controllers
  +-admin # subdirectory
      +-default_controller.php # This will pick up any calls to http://yoursite.com/admin. Obviously, this needs to be replaced with the name of your default controller.
      +-news.php # controller

http://yoursite.com/admin/ # Calls the default controller in the admin directory
http://yoursite.com/admin/news # Calls the news controller in the admin directory
#7

[eluser]iamzozo[/eluser]
Thx!
#8

[eluser]asylmottaket[/eluser]
Maybe modular seperation could be a nice alternative:

http://codeigniter.com/wiki/Modular_Extensions_-_HMVC
#9

[eluser]Phil Sturgeon[/eluser]
Good guide FinalFrag. This has been covered many times and everyone has their own favourite. For my previously posted breakdown of pro's and con's of different methods check out this post.
#10

[eluser]louis w[/eluser]
I have an application which has a front end and backend running on the same install (cms). As previously suggested, I just added an 'admin' folder to my controller dir. This houses all my admin controllers. MY front end is smaller because it just loads data from the models and passes it to the template library, so this is all contained in one controller in the base controller directory.

I tried doing this with an HMVC layout but found it to be overkill so I removed it a while back.

This is all accomplished with a routes:

Code:
$route['default_controller']     = "site";

// Route everything except admin calls to the site controller.

$route['admin(.*)']         = 'admin$1';

$route['(.*)']              = $route['default_controller'] . "/$1";




Theme © iAndrew 2016 - Forum software by © MyBB