CodeIgniter Forums
attempting to get a clear understanding of URI. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: attempting to get a clear understanding of URI. (/showthread.php?tid=31170)



attempting to get a clear understanding of URI. - El Forum - 06-09-2010

[eluser]dottedquad[/eluser]
for an example I have:
http://localhost/lsms/index.php/validate_livestock_form/species/1/alias/moe/parents/undefined/gender/male/breed/No/birthdate/2010-06-09/location/4/comment/commentsgohere

I would like to use $array = $this->uri->uri_to_assoc(3); to handle the URI. I've read the user guide, searched this forum, and search google for a clear example and I'm still confused. On google I've been running across pagnation examples multiple times. Can someone please point me to a clear example on how to use uri_to_assoc(). Thank You.

-rich


attempting to get a clear understanding of URI. - El Forum - 06-09-2010

[eluser]mddd[/eluser]
Can you explain what things you are confused about?
The basic idea is very simple: instead of writing
Code:
/oldfashioned_script.php?search=someword&sortby=name&page=2
you would write
Code:
/mycontroller/mymethod/search/someword/sortby/name/page/2

For the uri given above, you would get the following result:
Code:
$args = $this->uri->uri_to_assoc(); // this is the same as uri_to_assoc(3) because 3 is the default
// $args is now:
array('search'=>'someword', 'sortby'=>'name', 'page'=>2);

Why is this handy? Imagine you used a shorter uri like
Code:
/mycontroller/mymethod/someword/name/2

Now, if you wanted to see page 2 of a search for 'someword' but NOT specify a sorting order, how would you do that?
You would have to think of something like
Code:
/mycontroller/mymethod/someword/no-sorting/2
If you have many arguments that are optional, it would get messy. That's what uri_to_assoc is useful for.


attempting to get a clear understanding of URI. - El Forum - 06-09-2010

[eluser]dottedquad[/eluser]
[quote author="mddd" date="1276095392"]Can you explain what things you are confused about?[/quote]

my view code:
Code:
<?php

    class Validate_livestock_form_view extends Controller
    {
        
        function Validate_livestock_form_view()
        {
            parent::Controller();
        }
        
        function index()
        {
            $array = $this->uri->uri_to_assoc(3);

            echo "species: " . $array['species'];
            
            //set get variables to readable variables
        }
        
    }
?>

When I point my browser to:
http://localhost/lsms/index.php/validate_livestock_form/species/1/alias/moe/parents/undefined/gender/male/breed/No/birthdate/2010-06-09/location/4/comment/commentsgohere

I receive a 404 Page Not Found.

When I point my browser to http://localhost/lsms/index.php/index/species/1/alias/moe/parents/undefined/gender/male/breed/No/birthdate/2010-06-09/location/4/comment/commentsgohere

I no longer receive the 404, but receive a blank page. My echo also did not echo out species:

The 404 error and how to use and where to place uri_to_assoc() is confusing me.

-Rich


attempting to get a clear understanding of URI. - El Forum - 06-09-2010

[eluser]dottedquad[/eluser]
[quote author="mddd" date="1276095392"]Can you explain what things you are confused about?
[/quote]

Please disregard my first response to your question. I had to read over the user guide again on the controller and views to understand the page layout. My new code is as follows:

Controller code:
Code:
<?php
class Validate_livestock_form extends Controller
{
    
    function index()
    {

        $this->load->view('validate_livestock_form_view');
    }
    
}
?>

View code:
Code:
<?php

    $array = $this->uri->uri_to_assoc(3);

    echo "species: " .$array['species'];

?>

I shortened my URI so I can understand this better.

Current uri:
http://localhost/lsms/index.php/validate_livestock_form/species/1/

When I point my browser to the above address I receive a 404 Page Not Found error.

Why am I receiving a 404 and how do I fix this issue? I'm looking for a quick and usable code example that I can work from. The easiest way for me to learn is by pulling code apart. Thank you in advance!

-Rich


attempting to get a clear understanding of URI. - El Forum - 06-09-2010

[eluser]danmontgomery[/eluser]
From the user guide,

Quote:Typically there is a one-to-one relationship between a URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:

example.com/class/function/id/

http://ellislab.com/codeigniter/user-guide/general/routing.html

For the URL: http://localhost/lsms/index.php/validate_livestock_form/species/1/

CI is looking for:

Controller: validate_livestock_form
Function: species
Parameters: 1

Which doesn't exist. You have to include index/ so it's recognized as the function, or use CI's routing feature.


attempting to get a clear understanding of URI. - El Forum - 06-09-2010

[eluser]dottedquad[/eluser]
I forgot to include the function index in the URI. It all works as expected. finally :-)