Develop a sub-websites (micro websites) in Codeignitter

103 Views Asked by At


Let's say I have a website called http://mysite.dev

What I have:

So the urls of some pages will be below.

Home page: http://mysite.dev or http://mysite.dev/site/index or http://mysite.dev/home (site/index has been routed to home)

Edit Profile: http://mysite.dev/user/edit or http://mysite.dev/edit-profile (user/edit has been routed to edit-profile)

Admin Dashboard: http://mysite.dev/admin/ or http://mysite.dev/admin/index

Admin View Users: http://mysite.dev/admin/users or http://mysite.dev/admin/users/index

Admin Edit User: http://mysite.dev/admin/users/edit/1

So, basically my url pattern is as below.

http://mysite.dev/{module}/{controller}/{action}/{params}

What I need:

While keeping the main site as above, I need to have sub websites (micro websites) which will share the same login in the main site.

So that, the url pattern would be as below.

http://mysite.dev/{microsite}/{module}/{controller}/{action}/{params}

(microsite is empty means its the main website. There will no be any directory to represent the microsite since its just a reference as a name.)

That means, this microsite will not have sub directories since it the microsite name will be dynamic.

What I need to know:

How can I implement that url pattern either using .htaccess or url routing to develop this dynamic microsites.


Thank you!

1

There are 1 best solutions below

3
James Walker On

Okay I think I have got my head around this one!

Firstly, you will need to organise the main site and each microsite into their own subdirectories (for controllers, models, views, etc), such as:

application/controllers/main_site/module/etc
application/controllers/micro_one/module/etc
application/controllers/micro_two/module/etc

Then inside the routes folder you would do something similar to the following pseudo code:

Create array of microsite names (maybe get these from database)

Get the first URI segment

If URI segment is in microsites array
  $route['home'] = "{uri_segment/microsite_name}/module/controller";
else
  $route['home'] = "main_site/module/controller";

So for example your microsites array may be:

array('micro_one', 'micro_two')

Then the URL http://mysite.dev/micro_one/home would route to

$route['home'] = "micro_one/home/index";

I hope I have explained this enough for you to get started on your project!