Welcome Guest, Not a member yet? Register   Sign In
Building a simple pages controller with slugs
#1

[eluser]gh0st[/eluser]
I'm wanting to build simple controller for essentially a 5-page website that handles pages with slugs as a parameter, but I cannot get it to work.

Ideally, I want it to work like this:

somewebsite.com/pages/slug-goes-here/(other parameters go here)

Or even better, to forgo the whole /pages/ altogether, but I am not sure how to best address this, because CodeIgniter uses a structure as this:

/controllers/methods/parameters

The problem I have is that the default controller goes to index, and there is no method/function to handle the slugs.

So, if we have a controller that does this:

Code:
class Pages extends Controller
{
   function Pages()
   {
      parent::Controller();
   }

   function index($slug="")
   {
      print "<br>Slug: " . $slug;
      print "<br>". __FUNCTION__;
   }
}

If I try /pages/some-slug/ it will report a 404 error.

But how would you make it work so that it forgoes one, or even two steps?

Thanks
#2

[eluser]markup2go[/eluser]
did you set the default controller to pages in your routes.php?
#3

[eluser]gh0st[/eluser]
Hi there.

Yes, I set the default controller to pages. But this does not address the issue of what do you link to in a hyperlink?

If you put:

/pages/some-slug/

in a hyperlink it will not return the correct method/function because it does not exist.

I think the solution requires a custom routing, I've tried the following without success;

Code:
// tried both these ideas without success
$route['pages/(:any)'] = 'pages/index/$1';
$route['pages/(:any)'] = 'pages/index';
#4

[eluser]markup2go[/eluser]
If your rules are setup correctly (in your .htaccess file) then it should work.

Could you post your htaccess?

Code:
// tried both these ideas without success
$route['pages/(:any)'] = 'pages/index/$1';
$route['pages/(:any)'] = 'pages/index';

This seems redundant, as they both do the same things. However if this isn't even working then that leads me to my conclusion.

Link to whatever you'd like. In your example you would create anchors like
Code:
anchor("pages/$page->slug", $page->name);
#5

[eluser]gh0st[/eluser]
Hi there.

I have found a solution on this website, which I found to be working:

Code:
http://pinoytech.org/blog/post/codeigniter-route-everything-except-these-controllers

I created a php page called "pages.php" which has a switch for each of the 5 pages, and uses the slug as a parameter.

The routes were this:

Code:
$route['(:any)'] = 'pages/$1';
#6

[eluser]Aken[/eluser]
CodeIgniter isn't meant to function with a slug option on the index function of a controller. The Controller user guide page explains how to pass URI segments through to your controller functions (besides index). In your current set up, CI is looking for a function with the name of your slug under the Pages controller. When it doesn't find it, it gives the 404 error.

I'd recommend creating a second function, and allow the slug to be processed there. And then set up a route to forward any links with the structure of example.com/pages/slug to go to that function.
#7

[eluser]gh0st[/eluser]
Thanks for the idea, but as the website isn't too big. I find the pages controller I have which has a switch statement seems to be working.

Code:
class Pages extends Controller
{
    public function Pages()
    {
        parent::Controller();
    } // end function
    
    public function index($someslug="", $curPage=1)
    {
        if (is_empty($someslug)==true):
            redirect("/");
            exit;
        endif;
        
        
        /*
        * A list of pages on the website
        */        
        $someslug = strtolower($someslug);
        switch ($someslug)
        {
            case 'about-us':
                                // Do pagination computations (if required)
                // Load view with optional data
                $this->load->view("/pages/about-us");
            break;

                        // other page slugs go here.
                }
         } // end function    
} // end class




Theme © iAndrew 2016 - Forum software by © MyBB