1

For each user on my site, they have their own profile page with a list of articles. When the user clicks on one of the list items, an animation happens and then an AJAX call brings up a div that displays the article. At the same time, I use some javascript to force a URL change to reflect this:

http://www.example.com/UserName/Hello-World-Article

When the user clicks the back button in their browser, it calls the javascript function to animate going back to the listview state.

So far so good. But let's say the user enters the above URL into their address bar and presses enter, here is the question:

How do I pass the 'UserName' and 'Hello-World-Article' variables into Codeigniter and use them properly in this context?

3 Answers 3

9

Sorry, I was a little bit confused by the wording of your question, but I think I understand what you are trying to talk about.

First: Read the Docs on Controllers. Codeigniter documentation rocks.

Second: The URI's are routed directly to controller class names and their functions.

For example:

<?php

    class Profile extends CI_Controller {

        function item($username = NULL, $title = NULL)
        {
            if (is_null($username) || is_null($title)) redirect(base_url());

            // blah run code where $username and $title = URI segement
        }

     }

This will produce this URL:

 http://www.example.com/profile/item/username/whatever-i-want

Then you can remove item using routes in application/config/routes.php (docs):

 $route['(:any)'] = 'profile/item/$1';

Better way though (read ahead):

 $route['profile/(:any)'] = 'profile/item/$1';

Finally, this will create the URL you are looking for:

http://www.example.com/username/whatever-i-want

//http://www.example.com/profile/username/whatever-i-want

I might need to double check this for syntax errors, but it's the basics of how Codeigniter routing works. Once your URL is setup like that, you can do whatever you want to it with your JS.

However, I strongly advise against this method because routing a class like this will pretty much render the rest of the application/site useless, unless this is your only controller/function (probably not the case) you have. I think you would be better off with just having a class name in the URL one way or another.

Alternatively, you could also just use index() and $this->uri->segment() like so if you want to skip the routing in a non-conventional way.

<?php

class Profile extends CI_Controller {

    function index()
    {
        $username = $this->uri->segment(1);
        $title = $this->uri->segement(2);
    }
}

Hopefully, that makes sense and helps you with your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I'm using the alternative way.
0

how about sending only one parameter that is delimiter separated

Example

url = http://www.example.com/UserName_title

then in your controller just explode the parameter

class Profile extends CI_Controller {

    function item($param = "")
    {
        if ( $param == ""){ return false; }

        $param = explode('_', $param);
        $username = $param[0];
        $title = $param[1];
    }
 }

The fun part is that you can send as many parameters as you want without having to go technical.

PS: Make sure to choose the delimiter wisely so that it's not included in one of the parameters contents.

Comments

0

For those who is still struggling with this you can solve the above problem in codeigniter itself-

Your controller

class Mycontroller extends CI_Controller{
   public function my_method($arg1, $arg2){
     echo $arg1;
     echo "<br>";
     echo $arg2;
   }
}

Then your url will be

http://localhost/CIProjectFolder/Mycontroller/my_method/argument1/argument2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.