【问题标题】:CodeIgniter object controller not found找不到 CodeIgniter 对象控制器
【发布时间】:2018-05-24 17:57:40
【问题描述】:

我正在使用 codeIgniter 3.x。我加载了一个视图,其中包含一个提交给 User.php 控制器的表单,但是每当我点击提交按钮时,它都会重定向到这个 url "http://localhost/signup/Users/register" 并且它总是给出“未找到对象!在此服务器上未找到请求的 URL。"

以下是我的 .htaccess 文件

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /signup/index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /signup/index.php
</IfModule>

以下是 autload.php

中的自动加载助手
$autoload['helper'] = array('url','form');

以下是 config.php

$config['base_url'] = 'http://localhost/signup/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

以下是我在 Welcome.php 视图中打开和关闭表单的方式。

<?php echo form_open("Users/register", $attributes);?>
<?php echo form_close(); ?>

下面是Users.php控制器

class Users extends CI_Controller
{
public function __construct()
{
    parent::__construct();
    $this->load->helper(array('form','url'));
    $this->load->library(array('session', 'form_validation', 'email'));
    $this->load->database();
    $this->load->model('user_model');
    $this->load->view('user_registration_view');
}

function index()
{
    $this->register();
}
    function register()
{
    //set validation rules
    $this->form_validation->set_rules('fname', 'First Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
    $this->form_validation->set_rules('lname', 'Last Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
    $this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email|is_unique[user.email]');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|md5');
    $this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required|matches[password]|md5');

    //validate form input
    if ($this->form_validation->run() == FALSE)
    {
        // fails
        $this->load->view('user_registration_view');
    }
    else
    {
        //insert the user registration details into database
        $data = array(
            'fname' => $this->input->post('fname'),
            'lname' => $this->input->post('lname'),
            'email' => $this->input->post('email'),
            'password' => $this->input->post('password')
        );

        // insert form data into database
        if ($this->user_model->insertUser($data))
        {
            // send email
            if ($this->user_model->sendEmail($this->input->post('email')))
            {
                // successfully sent mail
                $this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are Successfully Registered! Please confirm the mail sent to your Email-ID!!!</div>');
                redirect('user/register');
            }
            else
            {
                // error
                $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error.  Please try again later!!!</div>');
                redirect('user/register');
            }
        }
        else
        {
            // error
            $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error.  Please try again later!!!</div>');
            redirect('user/register');
        }
    }
}

【问题讨论】:

  • 显示您的 user.php 控制器文件。
  • @urfusion 完成。
  • 第一名die('here'); 在您的__construct 中检查通话。
  • @urfusion 没有打印任何内容。
  • 试试这个htaccess RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L]

标签: php .htaccess codeigniter codeigniter-3


【解决方案1】:

您的htaccess 有问题,所以将其更改为默认值

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?/$1 [L]

在配置中

$root=(isset($_SERVER["HTTPS"]) ? "https://" : "http://").$_SERVER["HTTP_HOST"];
$root.= str_replace(basename($_SERVER["SCRIPT_NAME"]), "", $_SERVER["SCRIPT_NAME"]);
$config["base_url"] = $root; 

【讨论】:

  • 现在可以使用了,谢谢!我还更改了.htaccess 的位置。 config中的代码是做什么的?
【解决方案2】:

如果您的应用程序位于您的注册文件夹下(如您的 .htaccess 文件所示),那么您需要执行以下操作...

.htaccess 它与您的应用程序位于同一文件夹中。即与系统 index.php 文件相同的文件夹。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L] 

由于您的 URL 是 http://localhost/signup/,您已在 $config['base_url'] 中正确设置它应该可以工作。

我刚刚创建了一个虚拟测试站点并对其进行了测试。

让我知道这是否有效。

【讨论】:

  • 有效!问题是.htaccess 在应用程序文件夹中。
【解决方案3】:

文件名应与类名相同 Users.php Class Users extends CI_Controller

【讨论】:

  • &lt;?php echo form_open("User/register", $attributes);?&gt; 应该是users/register
  • 已完成但仍无法正常工作。我现在将编辑帖子。 @Riyenz
【解决方案4】:

我认为你在重定向时犯了错误......

替换: 重定向('用户/注册');

与: 重定向(“用户/注册”);或重定向('用户/注册','刷新');

【讨论】:

  • 主要问题是我的 .htaccess 文件在我的应用程序文件夹中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-18
  • 1970-01-01
  • 2022-08-13
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
  • 2016-10-12
相关资源
最近更新 更多