Codeigniter 仅路由到默认值

Codeigniter only routing to default

我目前正在学习使用 Codeigniter,我能够成功路由到默认控制器的索引方法,但是当尝试路由到另一个页面时(例如 localhost/results),我只被带走了回到默认页面,但 URL 更改为新的控制器或方法(例如,从本地主机到 localhost/results 显示与本地主机相同的页面)。

config.php 设置:

$config['base_url'] = '';
$config['index_page'] = 'index.php';

routes.php:

$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['result'] = 'result';

.htaccess 页面:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/ [L]

#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ index.php?/ [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/ [L]
</IfModule>

<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 index.php
</IfModule> 

控制器home.php:

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

class Home extends CI_Controller {


  public function index()
  {
    $this->load->view('home_view');

  }
}

控制器result.php:

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

class Result extends CI_Controller {


  public function index()
  {
    var_dump($this->input->post());
  }

}

查看home_view.php:

    <!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
  <form action="result" method="post">
    <input type="text" name="name" />
    <input type="submit" value="submit" />

  </form>
</body>
</html>

这可能只是部分答案,但是...

有两件事要做。

  1. 肯定给$config['base_url']设置一个值
  2. 设置$config['index_page'] = ''这在使用mod时很重要-按原样重写。

  1. (我知道我说了两件事。但想到了另一件事。)

像这样在表单的 action 属性中使用绝对值 URL 可能会获得更好的结果。

<form action= <?php echo base_url("result"); ?> method="post">

base_url() 依赖于 $config['base_url'] 的设置。您还需要在控制器中更早的地方执行以下操作。

$this->load->helper('url');
  1. (我知道,但我一直看到其他东西)... 4. 从 routes.php.
  2. 中删除 $route['result'] = 'result';

如果这些没有帮助,我建议您注释掉与保护系统和应用程序文件夹相关的 .htaccess 行,以确保这不是问题。

所以,我最终还是回到了旧版本的 Codeigniter。它现在似乎可以使用与以前相同的设置。感谢大家的回复和帮助。