调用我的一个codeigniter控制器返回404错误我的代码没问题

时间:2017-08-30 09:56:45

标签: php codeigniter-3

当我将我的网址http://localhost:8080/myScholarship/register发送到服务器时,我收到404错误。我的控制器名称为Register,文件名与Register.php一起保存。我正在调用index()函数。

这是我的控制者:

<?php
class Register extends CI_Controller {

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

    public function index()
    {
            $this->load->view('pages/index');
    }

    /****************************************create*************************************/
    public function create()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');

        //$data['title'] = 'Create a news item';

        $this->form_validation->set_rules('surname', 'Surname', 'required');
        $this->form_validation->set_rules('firstname', 'Firstname', 'required');
        //$this->form_validation->set_rules('othername', 'Othername', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required');
        $this->form_validation->set_rules('phonenumber', 'Phonenumber', 'required');
        $this->form_validation->set_rules('gender', 'Gender', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('confirmpassword', 'confirmpassword', 'required');

        if ($this->form_validation->run() === FALSE)
        {
            //$this->load->view('templates/header', $data);
            $this->load->view('index');
            //$this->load->view('templates/footer');

        }
        elseif($this->input->post('password') != $this->input->post('confirmpassword')){
            $data1 = "the password does not match";
            $this->load->view('index',$data);
        }
        else
        {
            $file_data = '';
            if((isset($_FILES['recentphoto']['name']) && !empty($_FILES['recentphoto']['name']))){
                $file_data = './uploads/'.$_FILES['recentphoto']['name'];
            }else{
                $file_data = './uploads/images.jpg';
            }
            $file = do_upload();
            if(isset($file['error'])){
                $data1 = $file['error'];
                $this->load->view('index',$data1);
            }else{
            $this->news_model->set_applicant($file['upload_data']);
            $succ = "please login with your account details";
            $this->load->view('index',$succ);
            }
        }
    }
    /*************************************end create***************************************/
    public function do_upload()
    {
            $config['upload_path']          = './uploads/';
            $config['allowed_types']        = 'gif|jpg|png|jpeg';
            $config['max_size']             = 100000;
            $config['max_width']            = 1024;
            $config['max_height']           = 768;

            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload('recentphoto'))
            {
                  return $error = array('error' => $this->upload->display_errors());

                    //$this->load->view('upload/upload', $error);
            }
            else
            {
                   return $data = array('upload_data' => $this->upload->data());

                    //$this->load->view('upload/upload_success', $data);
            }
    }
}
?>   

这是我的routes.php

<?php
$route['register'] = 'register';
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
?>

2 个答案:

答案 0 :(得分:0)

If you work in a server Linux/Unix you can remember that file names ar case sensitive, try with url else or change the name of controller to register.php without upper letter R

EDIT

Change yours routes to:

finally

Try.

答案 1 :(得分:0)

Route.php 文件中,还需要定义函数名(索引),请使用此路由。

$route['register'] = 'register/index';

而不是

$route['register'] = 'register';

如果它仍然不起作用那么可能是htaccess文件的问题,也尝试这样的URL。

http://localhost:8080/myScholarship/index.php/register

如果以上网址工作正常,则表示存在htaccess问题。