• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create dynamically controllers function name in codeigniter

#1
[eluser]beeLoop[/eluser]
Hello,
I am creating a fashion oriented site in codeigniter.
Now my requirement is: i need url in form of "http://example.com/user_name"

For-example: http://example.com/beeLoop.

So , what are the possible ways to create dynamically controllers function name in codeigniter.

#2
[eluser]alexwenzel[/eluser]
look at this thread . i posted a solution there already.

http://ellislab.com/forums/viewthread/227841/

#3
[eluser]beeLoop[/eluser]
With reference to your thread,

http://your.app/display/home
http://your.app/display/news
http://your.app/display/portfolio

But i don't need like this, it would be like

http://your.app/user_name1
http://your.app/user_name2

So Let me rephrase my query like:
So , what are the possible ways to create dynamically controllers in codeigniter, not controller functions.



#4
[eluser]alexwenzel[/eluser]
Routes.php is again the key to achieve this.

Example #1:

My prefered way.

routes.php

Code:
$route['user/(:any)'] = 'controller_user/display_user/$1';

controller_pages.php

Code:
class Controller_User extends CI_Controller {

public function display_user($username) {

// get content from database where users.name = $username

// display database content in a view

}

}

Now you can call:

http://your.app/user/$name


Example #2:

Route every call through a main controller / handler. Not that good i think.

routes.php

Code:
$route['(:any)'] = 'controller_main/index/$0';

controller_pages.php

Code:
class Controller_Main extends CI_Controller {

public function index($param) {

// check if $param is an existing username
// if so, display user-page

// if not, do whatever

}

}

Now you can call:

http://your.app/$name


Just 2 ideas of myself Smile

#5
[eluser]beeLoop[/eluser]
Example #2 seems the way to achieve what i want.
But it will route any controller to the the value i specified in my routes.php file in
$route['(:any)'] = 'controller_main/index/$0';

It will be a havoc for me and my site.

Is it possible to perform database operations in routes.php in codeigniter.

#6
[eluser]alexwenzel[/eluser]
[quote author="beeLoop" date="1350043780"]Example #2 seems the way to achieve what i want.
But it will route any controller to the the value i specified in my routes.php file in
$route['(:any)'] = 'controller_main/index/$0';

It will be a havoc for me and my site.

Is it possible to perform database operations in routes.php in codeigniter.
[/quote]

I dont think its possible to do DB operation there.

I have another idea for you. Look at hooks. So you can avoid touching routes.php, and avoid the mess resulting from a main controller.

http://ellislab.com/codeigniter/user-gui...hooks.html

I guess the pre_controller hook is perfect for your needs.

#7
[eluser]PhilTem[/eluser]
Nope, it's not possible to perform database queries in routes.php since the database library wasn't loaded at the time the routes are being read. However, you can route all your requests through a bootstrap-controller just like @alexwenzel suggested. But you need to be careful with extending CI_Controller in that case since you will get problems with the CI-singleton (which will no longer be a singleton but a doubleton (or whatever the real name for it is)).
You can have a look here
http://files.ignitedco.de/codeigniter/tu...uting.html

I wrote a little tutorial on how to achieve what you want. Please be aware it's not tested and just written off the top of my head and it does not support controllers in sub-directories. However it should give you a hint to what the second example looks like.

To add: I recommend using the second example only if you will have a load of dynamic pages, otherwise you should honestly go with the first example by @alexwenzel.


PS: You may of course also do some looping over the files found in APPPATH . 'controllers/' and add those manually to a big string like

Code:
$routes['(big|list|of|controllers|found|in|folder|APPPATHcontrollers)'] = $1;
$routes[(:any)] = 'bootstrap/route';

which will route example.com/big, example.com/list, ..., example.com/APPPATHcontrollers to the respective controller and everything else to your bootstrap/dynamic-pages controller.

#8
[eluser]Aken[/eluser]
Use the 404 override controller method.

#9
[eluser]beeLoop[/eluser]
[quote author="PhilTem" date="1350047105"]Nope, it's not possible to perform database queries in routes.php since the database library wasn't loaded at the time the routes are being read. However, you can route all your requests through a bootstrap-controller just like @alexwenzel suggested. But you need to be careful with extending CI_Controller in that case since you will get problems with the CI-singleton (which will no longer be a singleton but a doubleton (or whatever the real name for it is)).
You can have a look here
http://files.ignitedco.de/codeigniter/tu...uting.html

I wrote a little tutorial on how to achieve what you want. Please be aware it's not tested and just written off the top of my head and it does not support controllers in sub-directories. However it should give you a hint to what the second example looks like.

To add: I recommend using the second example only if you will have a load of dynamic pages, otherwise you should honestly go with the first example by @alexwenzel.


PS: You may of course also do some looping over the files found in APPPATH . 'controllers/' and add those manually to a big string like

Code:
$routes['(big|list|of|controllers|found|in|folder|APPPATHcontrollers)'] = $1;
$routes[(:any)] = 'bootstrap/route';

which will route example.com/big, example.com/list, ..., example.com/APPPATHcontrollers to the respective controller and everything else to your bootstrap/dynamic-pages controller.[/quote]

About requests through a bootstrap-controller, i need to dig a bit regarding this, as i need to exactly understand what i will be implementing in my project. But the idea to request through bootstrap-controllers seems interesting to me.

#10
[eluser]PhilTem[/eluser]
If you want to understand the usage of a bootstrap-controller, you should really consider starting to read through index.php and all files that are included in index.php and basically follow the business logic/flow of CI to see what a bootstrap controller can do and how you can use it best Wink


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.