1

I want to capture parameter from url on default controller but it give ma an error 404 page not found.

routes.php

$route['default_controller'] = 'MainBrain';
$route['MainBrain/(:any)'] = "MainBrain/index/$1";
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

MainBrain .php

class MainBrain extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
    }

    public function index() {
        echo 'Parameter value - ' . $this->uri->segment(3);
        $this->load->view('index', $data);
    }

}

Now when ever I try load url localhost:/myproject => This is loading my default controller and its view but when I try localhost:/myproject/pa8989 => This gives me 404 page not found error.

.htaccess :

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

So how can I get parameter form default parameter instead of 404 error ?

1 Answer 1

1

If you want localhost:/myproject/pa8989 to route to your function, then you need to change the route :

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

to

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

Otherwise, you'll need to access the function via localhost:/myproject/mainbrain/pa8989

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

1 Comment

Thank you for yyour answer but this is not working. Because if will call any other controller it will redirect to default controller only. Example - localhost:/myproject/othercontroller/ => Then it will redirect to default controller only.

Your Answer

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