Welcome Guest, Not a member yet? Register   Sign In
how do I know which link has been clicked on web page? - CI newbie Q
#7

[eluser]charlie spider[/eluser]
Hi Brian,

you might like the solution I use.

I snaged this little bit of code from an Elliot Haughin tutorial ( www.haughin.com/ ) that allows me to route all requests for webpages that don't have a corresponding controller/method to one catch-all controller.

This allows me to have dedicated controllers for specific things and then one for all of the regular generic pages of a website. In fact I only use specific controllers for the admin section of my sites. All of the public pages go to a controller called "pages". Once I hit that controller I can pull the relevent page info from the database, load whatever resources, content, templates, etc, that are required, and serve the page.

Here's the routing code:

Code:
$exclude = array ( '.', '..', 'index.html', 'pages.php' );

$handle = opendir(APPPATH . 'controllers/');

while (false !== ($file = readdir($handle)))
{
    if ( ! in_array($file, $exclude))
    {
        if ( ! is_dir ( APPPATH.'controllers/' . $file ) )
        {
            $file = substr($file, 0, strlen($file)-4);
        }
        $installed_modules[] = $file;
    }
}

closedir($handle);

foreach ( $installed_modules as $module)
{
    // ie: index.php/news/
    $route[$module] = $module;
    
    // ie: index.php/news/view/hello/
    $route[$module . "/(.*)"] = $module . "/$1";
}

$route["(.*)"] = "pages/index/$1";

So this code builds an array of all of the "other" controllers in your application/controllers directory so that requests for them can be met, and then routes everything else to "pages" via a wildcard.


Since all of my public pages go to the one controller, and I don't use any other methods for that controller, I can simply grab the last segment of the uri to find out what page is being requested, like this:

Code:
$current_page = $this->uri->segment_array( $this->uri->total_segments() );

whether a page is a subpage (or a subpage of a subpage) is set in the database, and I just build the full url for navigation links, but basically I only ever care what the last segment of the uri is.

This allows me to dynamically add as many pages as I want anywhere in the sitemap tree without having to create a new controller for each page.

On the admin side of things I basically do what WanWizard talked about. Specific controllers with multiple methods logically grouped together based on functionality.

So I use the typical CI one-to-one webpage-to-controller/method system for my admin side of things but wildcard routing for the public pages.

Hope that helps you out.


Messages In This Thread
how do I know which link has been clicked on web page? - CI newbie Q - by El Forum - 06-25-2010, 11:13 PM



Theme © iAndrew 2016 - Forum software by © MyBB