简体   繁体   中英

getting 404 error with codeigniter's controllers on wamp server 64

I'm using codeigniter on wampserver, i'm very new to to the model controller views and i'm trying to understand well how it's works. Currently, i'm trying to do a basic creation of a controller: In /application/controllers/pages.php i just made this:


<?php

    class Pages extends CI_Controller{
    public function one(){
        echo 'hello world';
    }
    }

?>


So, yes, this is very basic but i get stuck with an 404 error when i try to reach the method with chrome: http://localhost/test/pages/one/

I've looked on internet several tutorials that were doing the same thing as i do but they get the "hello world" in their web page.

I've found that it might be because i was not typing index.php in my URL but i've modified my config.php and my route.php to no longer have to type it:

config.php:


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

/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = '';


routes.php:


$route['default_controller'] = 'pages/view';
$route['pages'] = 'pages/$1';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

What bothers me the most is that when i'm using this codeigniter's example it works well and i don't get the 404 error:


class Pages extends CI_Controller{
public function view($page = 'home')
{
        if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
        {
                // Whoops, we don't have a page for that!
                show_404();
        }

        $data['title'] = ucfirst($page); // Capitalize the first letter

        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
}
}

If i try to create another function it will be totally ignore. I suppose that i have a problem with my routing files or a problem with codeigniter's installation but in case of, i prefer asking before reinstall all...

Is someone having an answer to my problem? Thanks a lot.

change your routes from

$route['default_controller'] = 'pages/view';
$route['pages'] = 'pages/$1';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

to

$route['default_controller'] = 'pages/one';
$route['pages'] = 'pages/$1';
$route['(:any)'] = 'pages/one/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

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