Welcome Guest, Not a member yet? Register   Sign In
Redirect all URL requests to only one controller
#1

[eluser]pedsa[/eluser]
I'm new in CodeIgniter but I loving it!
I would like to know if it is possible to redirect all URL requests to only one controller (ex: front_controller.php). With this approach I can control all requests and can call the page content directly from the DB without having to define several controller with different names.

For instance,
what I have:
www.mysite.com/about //need a about controller
www.mysite.com/contact //need a contact controller

what I need:
www.mysite.com/about //call the front_controller and this controller loads the about content from DB
www.mysite.com/contact //call the front_controller and this controller loads the contact content from DB
#2

[eluser]gRoberts[/eluser]
You need to create "routes" within your /config/routes.php file.

i.e.

Code:
$config['default_controller'] = 'front';
$config['about'] = 'front/about';
$config['contact'] = 'front/contact';

see http://ellislab.com/codeigniter/user-gui...uting.html for more information
#3

[eluser]WanWizard[/eluser]
Or simply are a route for ":any" that routes to 'front/index', and use the URI class to determine what segments are present and which data to fetch from the database.
#4

[eluser]pedsa[/eluser]
[quote author="WanWizard" date="1338218328"]Or simply are a route for ":any" that routes to 'front/index', and use the URI class to determine what segments are present and which data to fetch from the database.[/quote]

Thank you.

Here's the solution like you said:

In application/config/routes.php
Code:
$route['default_controller'] = "front_controller";
$route['(:any)'] = $route['default_controller'];

In application/controllers/front_controller.php
Code:
class Front_controller extends CI_Controller {

    public function index() {
$argv = $this->uri->segment_array();
$argc = count($argv);
echo "<br>Number of segments = ".$argc;
echo "<pre>";
print_r($argv);
    }

}

This approach certainly will slow down the page speed, but, in my opinion, it will increase the application flexibility. Do you know any other more effective way to handle this approach in CodeIgniter?
#5

[eluser]InsiteFX[/eluser]
You can also use _remap but it has some limitations!

CodeIgniter Users Guide - Controllers _remap()
#6

[eluser]pedsa[/eluser]
Now I'm facing another issue... In the index method of the controller class "front_controller" I need to call other method that belong to another controller class (the "action_controller") but, as far as I Google it, CodeIginter does not support this feature... This is true?
I'm trying to implement a very interesting PHP framework architecture proposed by Wei Cui in his paper "The Research of PHP Development Framework Based on MVC Pattern". This framework receive all the requests in the front controller and then this controller select the right controller which will handle with the entire action, the "action controller". I've worked with this architecture and it was very useful for the development and maintenance.
Any suggestion?
#7

[eluser]CroNiX[/eluser]
If you have code that needs to be shared between multiple controllers, try putting it in a library.
#8

[eluser]pedsa[/eluser]
[quote author="CroNiX" date="1338260732"]If you have code that needs to be shared between multiple controllers, try putting it in a library.[/quote]

Sorry but I'm new in this framework. How can I do this? Can you point to some article or thread in this forum that can help me? Sorry to bothering with this apparently simple question but I've already Google it but without success ...
#9

[eluser]CroNiX[/eluser]
Did you look in the User Guide?
http://ellislab.com/codeigniter/user-gui...aries.html
#10

[eluser]pedsa[/eluser]
I think I've solved the problem, however I don't know if I used the best tool of Codeigniter.
Here's my solution:
1) New helper "Load_controller_helper.php"
Code:
&lt;?php
     function load_controller($in_controller_to_load) {
  $controller_path = APPPATH . 'controllers/' . $in_controller_to_load . ".php";
  include $controller_path;
  $controller_to_load = explode( '/', $in_controller_to_load);
  $obj_temp = new $controller_to_load[count($controller_to_load)-1]();
  return $obj_temp;
     }
?&gt;

2) Calling the function
Code:
public function _remap ($ in_method, $ in_params = array ()) {
     $ this-> load-> helper ('Load_controller ");
     $ this-> authentication = load_controller ("Authentication");
     $ this-> Authentication-> test ();
}


I developed a helper because I don't need a Class. Although I think the best approach would be extending the CI_Controller class, I tried doing this by follow the wiki http://codeigniter.com/wiki/MY_Controller but the problem was to load the MY_Controller... Any suggestion to this problem?




Theme © iAndrew 2016 - Forum software by © MyBB