Welcome Guest, Not a member yet? Register   Sign In
Routes & Main Controller / Specific Controller
#1

Hello Fantastic CI community ! 

I'm actually stuck in my mind and i need your help if you have 5 minutes to give away in order to save my soul  Big Grin

I need a route like that : 

PHP Code:
$route['(:any)/(:num)'

But before to load a controller, i need to know which one and for that i need to check the info in the database regarding the ID i got (= (:num)). 
Cause i have a main table to list all the pages of the projects (around 100.000 pages) and in this main table, it's specified the related content table for each one. 

So first my idea was to do :

PHP Code:
$route['(:any)/(:num)'] = 'pages/pages_lookup_by_id/$2'

The Pages Controller is loaded and i can get all the informations, but after i don't know how to move to the specific controller (for example the Recipes Controller) cause there is no $this->load->controller() or something like that.

After, i had the idea to use a callback function in the routes file, to define the specific controller i need to load it directly. But it seems the callback function can not be used this way...

So i'm stuck Big Grin

Please save me !
Thanks a lot !
Reply
#2

Redirct('ControllerName');
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(10-27-2017, 09:07 AM)InsiteFX Wrote: Redirct('ControllerName');

Thanks, but it's not really what i'm looking for. 
I don't want to make a redirect. Just to find a way to make a SQL request before to load the controller regarding the kind of content.
Or any solution to reach the same result.
Reply
#4

A controller does not call another controller, that makes no sense, apart from with a redirect.

The routes just link a url format to a different controller/method/args request. So you have a route calling a controller, then because of the data collected based on the original args, you want to call another controller that is somehow dependent on the information originally collected, but without a redirect? This seems remarkably circular to me, and I would only ask why?

If you create a template library, your original rerouted controller could pass the page info to the template library, and the template library could set the view based on the data it receives. This is one option.

Another would be that the original rerouted controller calls a view related to the information you got from the database.

A third option would be to redirect (not sure why you are against this).

A fourth option might be to use WireDesignz HMVC where you could call the module 'recipes' from within your first controller. Although I no longer use HMVC myself as it often causes as many problems as it solves.

I think InsiteFX was correct to suggest that you redirect based on the information you have collected. This is clean, simple and easy to implement. Then your recipes controller can really be fully independent and dedicated to dealing with recipes.

Hope that helps,

Paul.
Reply
#5

(This post was last modified: 10-27-2017, 11:12 AM by Coool6.)

(10-27-2017, 10:52 AM)PaulD Wrote: A controller does not call another controller, that makes no sense, apart from with a redirect.

The routes just link a url format to a different controller/method/args request. So you have a route calling a controller, then because of the data collected based on the original args, you want to call another controller that is somehow dependent on the information originally collected, but without a redirect? This seems remarkably circular to me, and I would only ask why?

If you create a template library, your original rerouted controller could pass the page info to the template library, and the template library could set the view based on the data it receives. This is one option.

Another would be that the original rerouted controller calls a view related to the information you got from the database.

A third option would be to redirect (not sure why you are against this).

A fourth option might be to use WireDesignz HMVC where you could call the module 'recipes' from within your first controller. Although I no longer use HMVC myself as it often causes as many problems as it solves.

I think InsiteFX was correct to suggest that you redirect based on the information you have collected. This is clean, simple and easy to implement. Then your recipes controller can really be fully independent and dedicated to dealing with recipes.

Hope that helps,

Paul.

So if i use the redirect solution, it means if the user goes to domain.com/title-title-title/240 he's automatically redirect to domain.com/controller/240 ? From a user/SEO view, it's the worse idea no ? 

And i don't want to call another controller, it's just because the routes system of CI is pretty restricted. Maybe the solution is to use MY_Router to override it ? 

---

To explain my issue in a different way, imagine you have URL like that : www.superwebsite.com/my-super-content (to have super friendly URL)
knowing that my-super-content can be a recipe, a blog post, a forum post, etc. It can use a lot of different controller but you need to check first in the database to know which one you need. How you will do that ?
Reply
#6

I found a solution which works, i added that at the end of Routes.php

PHP Code:
require_once( BASEPATH .'database/DB.php');
$db =& DB();
$query $db->get_where('pages', array('id' => $this->uri->segment(2)));
$controller $query->row_array()['content_table'];
$route['(:any)/(:num)'] = $controller.'/index/$2'

but i'm still interested if you have any better idea.
Reply
#7

You can also create a MY_Router inside application/core/MY_Router.php. It's at least a little cleaner.

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
MY_Router extends CI_Router {
    
    public function 
__construct($routing NULL)
    {
        
parent::__construct($routing);
        
        require_once(
BASEPATH.'database/DB.php');
        
$db =& DB();
        
        
$query $db->select('directory,controller,method')->get_where('pages', array('id' => $this->uri->segment(1)));
        
$routing $query->row_array();
        
        empty(
$routing['directory'])  OR $this->set_directory($routing['directory']);
        empty(
$routing['controller']) OR $this->set_class($routing['controller']);
        empty(
$routing['method'])     OR $this->set_method($routing['method']);
    }


You will need to set a default controller if there aren't any matches, or you will get 404.
PHP Code:
$route['(:any)'] = 'welcome'
Reply
#8

(This post was last modified: 10-28-2017, 08:16 AM by PaulD.)

I would have my controller decode the url, getting the page info. Then submit that to my template library. That library would set the page layout according to the page_type, ie blog post, recipe etc. Then output the views.

I think if the recipe page type needed additional data, that would be done by the controller calling the appropriate library or model (recipe_library or blog_library) to deal with data needed for recipe pages, or blog pages etc. Then pass all the data to the template library that would sort out the required views.

However, your super friendly url is not that great IMHO. I would prefer something like

Code:
http://www.superwebsite.com/recipes/my-super-recipe
or
http://www.superwebsite.com/blog/my-super-post
or
http://www.superwebsite.com/forum/my-super-comment

That would mean that my forum has its own controller, the recipe section too etc. and my site becomes much easier to maintain, to debug and to follow what it going on. No super controller decoding urls and calling other sub-controllers or libraries or models etc etc.

Paul

PS Just wanted to add that there is not 'right' answer to this, didn't want the above comment to come across in the wrong way. Everyone does these sort of structural things differently, which is partly why template engines or friendly_url systems are never right for most people. Whatever works for you. At least CI lets us do it whatever way we want...
Reply
#9

He can also use the URI Class and get the segments.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB