被找到但未找到的 CodeIgniter 控制器难住了

发布于 2024-10-11 02:09:13 字数 2055 浏览 3 评论 0原文

好吧,我承认。我是 CI 新手。我正在做一些非常基本的错误,我已经做了几个小时,但找不到我的错误。

我有一个名为登录的控制器。它位于我的控制器文件夹的资源文件夹中。它加载一个名为login_form 的视图。但对于我来说,如果我访问domain.com/resources/login 或domain.com/resources/index,它就不会加载。但我可以通过一条路线到达它:

$route['engineering-resources/login'] = "resources/login";

即使我以这种方式到达它,也找不到我的表单操作。这是我的表单:

<?php 
    echo form_open('resources/login/validate_credentials'); //folder/controller/method.  I think this is where my problem is
    echo form_input('username', 'Username');
    echo form_password('password', 'Password');
    echo form_submit('submit', 'Login');
    echo anchor('login/signup', 'Create Account');
    echo form_close();
    ?>

路径是控制器文件夹中的资源文件夹,控制器是使用 validate_credentials 方法的登录控制器。这是我的登录控制器的相关部分:

class Login extends Controller {

    function index()
    {
        $data['title'] = 'Engineering Resources | Login';
        $data['main_content'] = 'resources/login_form';
        $this->load->view('templates/main.php', $data);

    }

    function validate_credentials()
    {       
        $this->load->model('login/membership_model');
        $query = $this->membership_model->validate();

        if($query) // if the user's credentials validated...
        {
            $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => true
            );
            $this->session->set_userdata($data);
            redirect('resources/members_area');
        }
        else // incorrect username or password
        {
            $this->index();
        }
    }

索引函数在我使用路由时起作用,但在我使用上述domain.com路径时不起作用。我认为这就是它找不到 validate_credentials 方法的原因。我做错了什么?

这是main.php

<?php $this->load->view('includes/header.php'); ?>

<?php $this->load->view('includes/nav.php'); ?>

<?php $this->load->view($main_content); ?>

<?php $this->load->view('includes/footer.php'); ?>

OK, I admit it. I'm a CI newbie. I'm doing something really basic wrong and I've been at it for a couple of hours and can't find my error.

I have a controller called login. It's in the resources folder in my controllers folder. It loads a view called login_form. But for the life of me it will not load if I go to domain.com/resources/login or domain.com/resources/index. But I can get to it via a route:

$route['engineering-resources/login'] = "resources/login";

Even when I get to it this way, my form action isn't found. Here is my form:

<?php 
    echo form_open('resources/login/validate_credentials'); //folder/controller/method.  I think this is where my problem is
    echo form_input('username', 'Username');
    echo form_password('password', 'Password');
    echo form_submit('submit', 'Login');
    echo anchor('login/signup', 'Create Account');
    echo form_close();
    ?>

The path is my resources folder in my controllers folder, and the controller is the login controller using the validate_credentials method. Here is the pertinent part of my login controller:

class Login extends Controller {

    function index()
    {
        $data['title'] = 'Engineering Resources | Login';
        $data['main_content'] = 'resources/login_form';
        $this->load->view('templates/main.php', $data);

    }

    function validate_credentials()
    {       
        $this->load->model('login/membership_model');
        $query = $this->membership_model->validate();

        if($query) // if the user's credentials validated...
        {
            $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => true
            );
            $this->session->set_userdata($data);
            redirect('resources/members_area');
        }
        else // incorrect username or password
        {
            $this->index();
        }
    }

The index function works when I use the route, but not when I use the above domain.com paths. I assume that is why it cannot find the validate_credentials method. What am I doing wrong?

Here is main.php

<?php $this->load->view('includes/header.php'); ?>

<?php $this->load->view('includes/nav.php'); ?>

<?php $this->load->view($main_content); ?>

<?php $this->load->view('includes/footer.php'); ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各空 2024-10-18 02:09:13
$route['engineering-resources/login'] = "resources/login";

这不会路由到 engineering-resources/login/validate_credentialsresources/login/validate_credentials

你应该有这样的东西:

// not tested
$route['engineering-resources/login/(\S*)'] = "resources/login/$1";

还有一件事是,如果你使用路由,您也应该使用您视图中的路线..

echo form_open('engineering-resources/login/validate_credentials');
$route['engineering-resources/login'] = "resources/login";

This does not route to engineering-resources/login/validate_credentials to resources/login/validate_credentials

You should have something like this:

// not tested
$route['engineering-resources/login/(\S*)'] = "resources/login/$1";

One more thing is that if you are using routes, you should use the routes from your views too..

echo form_open('engineering-resources/login/validate_credentials');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文