Codeigniter问题求助:URL未找到+登录后无法跳转至book.php视图
Hey there, let's work through your CodeIgniter issues one by one—this framework has a few gotchas when you're starting out, but we'll get everything working smoothly.
localhost/reservation/ This error usually boils down to URL routing or server configuration missteps. Here's what to check step by step:
Verify your base URL setting
Openapplication/config/config.phpand make surebase_urlpoints to your project's root. If your CodeIgniter project lives in a folder namedreservation, it should look like:$config['base_url'] = 'http://localhost/reservation/';Enable Apache's rewrite module
If you're using XAMPP/WAMP, open your Apachehttpd.conffile and uncomment the lineLoadModule rewrite_module modules/mod_rewrite.so(remove the#at the start). Restart Apache after this change—this is required to removeindex.phpfrom your URLs.Check your .htaccess file
Make sure your project root has an.htaccessfile with the standard CodeIgniter rewrite rules:RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]This tells Apache to route all requests through CodeIgniter's
index.phpwithout showing it in the URL.Confirm your controller exists
You need a controller namedReservation.php(capitalized first letter) inapplication/controllers/with anindex()method that loads your login view. Example:class Reservation extends CI_Controller { public function index() { // Load the login view when accessing localhost/reservation/ $this->load->view('login'); } }
book.php View Your current form has no action attribute, so it submits to the same page without triggering the right controller logic. Here's how to fix it:
Update your form's action attribute
Modify your form to point to a dedicated controller method that handles login and loads the book view. Use CodeIgniter'sbase_url()helper to avoid hardcoding URLs:<form class="" method="POST" action="<?php echo base_url('reservation/login'); ?>"> <div> <button class="btn btn-primary" name="login">Login</button> </div> </form>Add the login method to your Reservation controller
InReservation.php, add alogin()method to process the form submission and load the book view. You can add credential validation logic here later, but for now, this will handle the redirect:public function login() { // Optional: Add login validation logic here (e.g., check username/password) // Load the book view after login $this->load->view('book'); }Enable the URL helper
To usebase_url(), you need to load the URL helper. You can autoload it inapplication/config/autoload.php:$autoload['helper'] = array('url');Or load it dynamically in your controller's constructor:
public function __construct() { parent::__construct(); $this->load->helper('url'); }Double-check your view files
Make surelogin.phpandbook.phpare both located inapplication/views/—CodeIgniter won't find them if they're in the wrong folder or named incorrectly.
Give these steps a shot, and if you run into any specific snags (like new error messages or missing files), feel free to share more details!
内容的提问来源于stack exchange,提问作者Emjey23

