简体   繁体   中英

Error 404 despite that page exists Codeigniter

I have something like website.com/profile/nameofuser that is working. But if I have website.com/profile/_nameofuser I get 404 error also website.com/profile/nameofuser_ or website.com/profile/nameof_user is working. It's not something related to accepted characters, but what's the problem?

class Profile extends CI_Controller {

public function __construct() 
{       
    parent:: __construct();
    $this->load->model('Profile_model');  
    $this->load->helper(array('url', 'form', 'htmlpurifier'));
}

public function index() {
    $this->load->library('form_validation');

    if(getUserData($this->uri->segment(2), "ID") < 0) {
        $this->session->set_flashdata('error', 'Profil inexistent.');
        redirect(base_url());
    }

    if (!is_cache_valid(md5('profile' . $this->uri->segment(2) . ''), 300)){
            $this->db->cache_delete('profile', $this->uri->segment(2));
        }

    if(getUserData($this->uri->segment(2), "ID") > 0) {

        /* some mysql queries.. */
        }

        $data["main_content"] = 'profile/profile_view';
        $this->load->view('includes/template.php', $data); 
    } else {
        $this->session->set_flashdata('error', 'Profil inexistent.');
        redirect(base_url());
    }
}
function _remap($method,$args)
{
    if (method_exists($this, $method))
    {
        $this->$method($args);  
    }
    else
    {
        $this->index($method,$args);
    }
}
}

Here is my profile controller. I really don't know what's the problem. If I enter an invalid profile redirects with error flashdata so ti's ok. Maybe it's a remap problem?

1) Check the fie format in your view file.. if it is html file that mean you cannot call it without its format for example if your file is php format have name home-view.php you can call it as

$this->load->view('home-view');

but if it is html file then the name in home-view.html so you have to call it with its exstention as

$this->load->view('home-view.html');

your call to /profile/nameofuser is missing a fundamental from the MVC architecture.

You need to call a controller/method combination (in CI, your basic url is domain.com/controller/method)...

since you don't have a specific method for each user within the profile controller, which is actually a good thing, you need a method within the controller that handles your users. You already have it, it's called index

If you point your URL to /profile/index/nameofuser and change $this->uri->segment(2) to $this->uri->segment(3) you should get it to work

give it a try and let me know

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM