1

I tried to encode the ID of a journal in the URL of my Code Igniter application and the retrieve it in my controller. My end goal is to access the page http://mysite.com/journal/3 and get to access a page containing details about the journal with ID 3.

In my journal.php controller file, I have

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Journal extends Controller {
    public function index($journalId) {
        $data['journalId'] = $journalId;
        $this->view->load('journalPage', $data);
    }
}
?>

In my journalPage.php view file, I have

This event has ID <?= $journalId ?>.

I wrote this rule in my routes.php file.

$route['journal/(:num)'] = 'journal/$1';

Here is the .htaccess file in my html public folder.

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>

However, when I go to mysite.com/journal/3, I get a 404 error. Why?

7
  • Do you have a .htaccess file set up? If not, you will need to access the site through its index page: mysite.com/index.php/journal/3 Commented Mar 19, 2012 at 21:15
  • I do have an .htaccess file. Controllers without data encoded in the URL (and whose methods do not accept parameters) are working. Also, neither mysite.com/journal/3 nor mysite.com/index.php/journal/3 are working. Interestingly, going to mysite.com/journal/ works. However, the text displayed is This event has ID . without an actual event ID. Commented Mar 19, 2012 at 21:17
  • Did you define rule at your routes.php ? Commented Mar 19, 2012 at 21:20
  • Following on @safarov's line of thought, posting the relevant pieces of .htaccess and config/routes.php might help us make a better guess. Commented Mar 19, 2012 at 21:23
  • 2
    why do you want to encode the ID? All your doing is adding another step for php to proccess Commented Mar 19, 2012 at 21:30

3 Answers 3

3
$route['journal/(:num)'] = 'journal/index/$1';

(:num) will become invalid if you encode

Edit: If you use CI's encryption class to encode your ID(pointless) you will need to modify it to make sure the string is uri safe.

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

1 Comment

Thank you, I had forgotten "index" in the string 'journal/index/$1'.
1

Based on what I see above this is just begginers mistake on CI routing.

You .htaccess is ok (you are just removing index.php from url).

On other 2 steps (you have problem in your controller and in your routes config).

First in controller, when creating new controller you should extend CI_Controller To make story short, this is how your journal.php file should like like:

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Journal extends CI_Controller {
    public function __construct() {
        parent::__construct(); // This is MUST!
    }
    public function index($journalId) {
        $data['journalId'] = $journalId;
        $this->view->load('journalPage', $data);
    }
}

Now when you have updated this, we come to routes config.

Config line you wrote only can confuse CI, nothing more, nothing less.

Structure of CI route shoud be like this:

$route['journal/(:any)'] = 'journal/index/$1';

This would redirect all traffic from journal/[ID] to controller named journal, to method named index with param [ID].

You must define index part in routing.

Try this, and everything should be working fine. Cheers.

Comments

1

First of all: You don't need to create a empty constructor (This is must [nothing!]). If you want to load some libraries, helpers or models at the beginning of all functions for that controller, so it is needed, otherwise it don't.

Other tip: Try to do not pass parameters on a controller function. You can catch and manipulate uri values with CodeIgniter's URI Class.

Just do this:

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Journal extends CI_Controller
{
    public function index()
    {
        $data['journalId'] = $this->uri->segment(2);
        $this->view->load('journalPage', $data);
    }
}

You better take a look on the URI Class. It is quite simple a handy! http://codeigniter.com/user_guide/libraries/uri.html

PS: Don't need to fix anything on routes too.

Hug

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.