Subkhan Sarif
Subkhan Sarif

Reputation: 469

Passing parameter to controller always get "Page Not Found" - CodeIgniter

I try to pass a parameter to my controller functions from my views but I always get "Page Not Found". I've been looking for any solutions possible for my problem from here, here, and here but I still couldn't find working solutions and I still don't know what could be the problem. I think I've set everything right, but it's not working. Please tell me if there's something I'm doing wrong.

Here is my controller: Controll.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Controll extends CI_Controller{

public function about($thing){
    $case = "About Us";
    $data['page'] = $case;
    $data['p'] = $thing;
    print($thing);
    //$this->load->view('/header/header',$data);
    $this->load->view('about_us',$data);
    $this->load->view('/footer/footer');
}
public function mountain_destination($thing){
    $data['page'] = "Mountain Destination";
    $data['p'] = $thing;
    //$this->load->view('/header/header',$data);
    $this->load->view('mountain_destination',$data);
    $this->load->view('/footer/footer');
}
}

?>

Here's my routes setting:

$route['(:any)'] = "controll/$1";
$route['default_controller'] = "controll";
$route['404_override'] = '';

And here's one of my script called by onClick that tries to call that controller function:

function goTab(thing,tab){
    switch(thing){
        case "about":
            $body.load("<?php echo site_url('about'); ?>/"+$(tab).text().toLowerCase().replace(/ /g,''));
        break;
        case "mountain":
            $body.load("<?php echo site_url('mountain_destination'); ?>/"+$(tab).text().toLowerCase().replace(/ /g,''));
        break;
    }

My script runs well and it produces right url. For example, I put parameter "myteam" into "goTab" function, it produces link like this: "http://127.0.0.1/about/myteam". This link's supposed to call "about" function inside my controller and pass "myteam" as parameter. But instead it returns "Page Not Found". And when I try to call "about" function without any parameters like this: "http://127.0.0.1/about", I get missing argument error like this:

Severity: Warning

Message: Missing argument 1 for Controll::about()

Filename: controllers/Controll.php

Line Number: 22

Please help me. Thanks in advance.

Upvotes: 2

Views: 2019

Answers (2)

Craig
Craig

Reputation: 1823

You're not passing it to your about method. You're looking for a method called myteam within your Controll controller.

Amend your Routes to this;

$route['about/(:any)'] = 'controll/about/$1';
$route['mountain_destination/(:any)'] = 'controll/mountain_destination/$1';

Hope this helps.

This answer of mine might also help you;

Codeigniter Menu from Database

Upvotes: 2

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

You can create a variable and pass it within constructor and simply use that whenever you need as

class Controll extends CI_Controller {

    private $thing;

    public function __construct() {
        parent::__construct();
        $this->thing = $this->uri->segment(3);
    }

    public function about() {
        $data['p'] = $this->thing;
        echo $this->thing;
    }

    public function mountain_destination($thing) {
        $data['p'] = $this->thing;
        echo $this->thing;
    }

}

Upvotes: 0

Related Questions