Controller question

#1
[eluser]Assim[/eluser]
Let's say I wanted to create controller like this:
http://example.com/pages to load page list
http://example.com/pages/something to load "something"

Well here's the code that I wrote, I'm using the remap function and taking $PageSlug as the second URI segement. In the above example, "something" was the $PageSlug
Code:
<?php

class Pages extends Controller {

    function Pages()
    {
        parent::Controller();

        // Load language file
        $this->lang->load('website');
    }
    
    function _remap($PageSlug)
    {
        if(!$PageSlug) {
            // Load page list if no page supplied in URI
            // http://example.com/pages
        }
        else
        {
            // Load a page according to page slug
            // http://example.com/pages/something
        }
    }
}

/* End of file pages.php */
/* Location: ./system/application/controllers/pages.php */

In the if statement, I can only load the page and not the page list.

I tried writing it in the following ways:
if(!$PageSlug)
if(empty($PageSlug))
if($PageSlug = '')

But still I can't load the page list which is when the $PageSlug is not provided. Does anybody have the solution to this problem?

#2
[eluser]Ki[/eluser]
Keep in mind that (depending on your rerouting) the CI follows controller/function method in its uri calls. So, if you want to call pagelist, you need to have a function within your controller called page_list(or something similar)... so when your uri looks like this http://www.examples.com/pages/page_list, this will call the function page_list within your controller called Pages. Or you can build a routing to have everything after pages passed as variable, but then you may want to have something like http://www.examples.com/pages/page_list/1, this way your pages controller will have a uri array as [page_list]=1 and then you can use

if( !empty(page_list) ){ do someting }

#3
[eluser]Assim[/eluser]
[quote author="samota" date="1266803131"]Keep in mind that (depending on your rerouting) the CI follows controller/function method in its uri calls. So, if you want to call pagelist, you need to have a function within your controller called page_list(or something similar)... so when your uri looks like this http://www.examples.com/pages/page_list, this will call the function page_list within your controller called Pages. Or you can build a routing to have everything after pages passed as variable, but then you may want to have something like http://www.examples.com/pages/page_list/1, this way your pages controller will have a uri array as [page_list]=1 and then you can use

if( !empty(page_list) ){ do someting }[/quote]
But I'm using the _remap function as described here:
http://ellislab.com/codeigniter/user-gui...#remapping

So it takes the controller as the first segment, and the other segment are taken as parameters in the _remap function.

I can make it as http://example.com/pages/list instead but I'd prefer http://example.com/pages instead since it's easier to type in the address bar.

#4
[eluser]Ki[/eluser]
Yeah... it seems that you got quite a challenge. Try using print to see what uri serves to the controller. One other suggestion is to use isset('something'). If this 'something' is set in uri, then you output function 'something', else you serve the list. I also discovered that if you set re-routing and it looks like this http://example.com/pages/list, it may serve an empty uri array, such as
  • =''. If you really need to define something, try http://example.com/pages/list/yes, this will serve
    • ='yes' and fail if( empty($list) )

      Sorry if I misunderstood your problem. post more examples of what you are trying to accomplish, as well as your re_routing code

#5
[eluser]Assim[/eluser]
Thanks mate, it works now.

This is how the controller looks like:
Code:
function _remap($PageSlug)
{
    if($PageSlug = 'list') {
        // Load page list
    }
    else
    {
        // Load a page
    }
}

And I added the following in routes.php
Code:
// Route /pages to /pages/list
$route['pages'] = "pages/list";

Now I access this page as http://example.com/pages and http://example.com/pages/list

Thanks again for the idea. Wink


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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