CodeIgniter form_open没有命中控制器

时间:2013-06-16 10:05:47

标签: codeigniter .htaccess routes codeigniter-2

大家好我是CodeIgniter框架的新手。我试图开发一个包含表单的页面。

这是视图


<?php echo form_open('Site/check');?>

User Name: <?php   echo form_input(array('id'=>'Username' ,'name'=>'username' ,'class="input-medium " ')); ?> 
Add User:<?php   echo form_submit('submit','Add User','class="btn btn-primary"');?> 
<?php echo  form_close();?>


这是控制器


<?php class Site extends CI_Controller{

  function login() {
      $data['records']="BHOOM!!!";
      $this->load->view('login',$data);
  }

  function check() {
     $this->load->view('showmessage');
  }

}
?>

showmessage是一个PHP页面,在<h1>标签中有一个问候语。 我已经用谷歌搜索并搜索了类似的问题,但我没有得到任何解决方案

基本网址为localhost/ci/index.php

错误

无法加载请求的文件:showmessage.php

的.htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|js|css|img\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

3 个答案:

答案 0 :(得分:0)

Here是直接来自EllisLab的教程。按照步骤操作,您将使表单正常工作。

最常见的错误,

  1. 未加载form帮助
  2. htaccess.设置错误,或路径错误(大写字母等)。
  3. config.php未正确设置
  4. 未将数据传递给视图($this->load->view('some_view', $data);
  5. 因此,如果存在错误消息,则您没有向我们提供错误消息,但社区没有任何可能的帮助。

    有关更多调试信息,请启用codeigniters profiler($this->output->enable_profiler(TRUE);),使用PHP本机函数var_dump($variable);

    确保E_ALL

    中的error reporting设置为index.php

    修改

    确保您的/config/config.php

    /*
    |--------------------------------------------------------------------------
    | 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'] = "";
    

    使您的.htaccess此文件位于项目的根目录中,例如。 /ci/.htaccess

    <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /ci/
    
      # Reenable after getting ssl cert
      # RewriteCond %{HTTPS} off
      # RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
    
      # Removes access to the system folder by users
      RewriteCond %{REQUEST_URI} ^system.*
      RewriteRule ^(.*)$ /index.php?/$1 [L]
    
      # When your application folder isn't in the system folder This snippet prevents user access to the application folder
      RewriteCond %{REQUEST_URI} ^application.*
      RewriteRule ^(.*)$ /index.php?/$1 [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?/$1 [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.
      ErrorDocument 404 /index.php
    </IfModule>
    

答案 1 :(得分:0)

只需使用此类代码进行试用

视图

<?php 
echo form_open('controllet_name/method_name'); 
form_input(array('id'=>'Username' ,'name'=>'username' ,'class="input-medium " '));
form_close();
?>

控制器

()
{
    $name=$this->input->post('username');
echo $name ;
    }

答案 2 :(得分:0)

我觉得你的RewriteRule错了。 尝试: RewriteRule ^(。*)$ / ci/index.php/$1 [L]