简体   繁体   中英

Controller 404 issue in codeigniter

I am trying to call my a controller from another controller using redirect like so..

Welcome Controller

public function login(){
    $rules=array(
        array(
            'field'=>'username',
            'label'=>'Username',
            'rules'=>'required|trim'
        ),
        array(
            'field'=>'password',
            'label'=>'Password',
            'rules'=>'trim|required'
        ),
    );
    $this->form_validation->set_rules($rules);
    if($this->form_validation->run()==TRUE){
        $data=array(
            'username'=>$this->security->xss_clean($this->input->post('username')),
            'password'=>$this->security->xss_clean($this->input->post('password'))
        );
        $account_details=$this->users_model->getCredentials($data);

        if($account_details->num_rows()==1){
            $account_details=$account_details->result();
            $this->session->set_userdata(array('username'=>$account_details[0]->username));
            redirect(site_url('navigation'),'refresh');
        }else{
            $this->session->set_flashdata('message','Wrong username or password.');
            $this->show_login();
        }
    }else{
        $this->session->set_flashdata('error',validation_errors());
        $this->show_login();
    }
}

Navigation Controller

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

Class Navigation extends CI_Controller{

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

    public function index(){
        $data['links']=$this->Users_access_model->get_links($this->session->userdata['username']);
        $this->load->view('common/header');
        $this->load->view('common/view_navigation');
        $this->load->view('common/scripts');
    }
    }

     ?>

this comes out in my addressbar

http://localhost/piercapitan/index.php/navigation

an I get this

404 Page Not Found

The page you requested was not found.

This is in my config

$config['base_url'] = 'http://localhost/piercapitan/';

This is my .htaccess

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

I have used the redirect before but I was using it from my windows machine but when I transferred to Ubuntu, that is the time that I get this issue. Thank you.

Change from

$config['base_url'] = 'http://localhost/piercapitan/';

to

$config['base_url'] = (isset($_SERVER['HTTPS']) ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . preg_replace('@/+$@', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';

$config['base_path'] = $_SERVER['DOCUMENT_ROOT'] . preg_replace('@/+$@', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';

Also change from

redirect(site_url('navigation'),'refresh');

to

redirect('/navigation','refresh'); 

or

redirect(base_url('navigation'));

You will need to autoload the url helper in the file 'application/config/autoload.php'

$autoload['helper'] = array('url');

just try this in

application/config/config.php

$config ['index_page'] = '';

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