【问题标题】:codeigniter controller function issuecodeigniter 控制器功能问题
【发布时间】:2012-04-06 16:13:18
【问题描述】:

我是 Codeigniter 编程的新手。我通过简单地编辑我的默认控制器欢迎启动 CI。我在控制器中添加了一个函数,该函数在单击提交按钮时被调用。我没有在默认框架或配置文件中进行任何修改。但是在单击提交按钮时,应用程序会返回 404 响应。 未找到 在此服务器上找不到请求的 URL /login/welcome/main。 Apache/2.2.14 (Ubuntu) 服务器在 localhost 端口 80

主要功能是我在欢迎控制器中写的。谁能告诉我出了什么问题。我的 Codeigniter 版本是 2.1.0。 我重新安装了 Apache 和 PHP。但是没用还是一样。来自控制器的代码和下面的欢迎页面。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(E_ALL);

class Welcome extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -  
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $this->load->view('login_page');
        //$this->load->view('phpinfo');
    }

    public function main()
    {

    $this->load->view('phpinfo');

    }

欢迎页面是

<div id="container">
<h1>Ignitor</h1>

<div id="body">

    <code>Please Login to continue. </code>


    <div id="login_form">
    <?php echo form_open(base_url().'welcome/main');?>
    <ul>
    <li>
    <label>Username </label>
    <?php echo form_input(array('id' => 'username', 'name' => 'Username'));?>
    </li>


    <li>
    <label>Password </label>
    <?php echo form_password(array('id' => 'password', 'name' => 'Password'));?>
    </li>


    <li> <?php echo form_submit(array('id'=>'submit'),'Let me in' )?></li>
    </ul>

    <?php echo form_close();?>

    */ 

【问题讨论】:

  • 你必须像这样放置表单动作。 login/welcome/main/ 你必须在你的配置文件中设置 base_url
  • 对不起。我没有完全理解你。我已经正确设置了 base_url() 函数。把它放在这里是什么意思..?感谢回复

标签: php model-view-controller function codeigniter controllers


【解决方案1】:

您是否正确命名了欢迎页面视图文件?换句话说,您的控制器要求 login_page_view.php(或者至少我相信这是默认的文件名约定)基于代码行位于 views 文件夹中:

$this->load->view('login_page');

【讨论】:

  • 默认名称是welcome_page.php,我将其重命名为login_page.php。相同的数据被转移到另一台计算机中,它可以正常工作。控制器和功能,一切正常。感谢您的回复。
【解决方案2】:

您正在跳过 URL 中的 index.php,这意味着您希望 .htaccess 文件将请求路由到 CodeIgniters 主 index.php。

使用 URL /login/index.php/welcome/main 检查您的 .htaccess 文件是否有问题或不存在。如果您重命名或移动了它所在的目录以“登录”,您可能忘记更新它。

“登录”目录中的 .htaccess 应该类似于 -

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /login/index.php/$1 [L]

如果它正在查找主 index.php 并且错误是由于视图/模型/控制器命名问题引起的,您会从 CodeIgniter 而不是 Apache 获得 404。

【讨论】:

  • 谢谢,丹。这次真的奏效了。非常感谢。 /login/index.php/welcome/main 工作。尽管我还没有真正理解这个问题,但我很高兴它成功了。你能再解释一下吗?
  • CodeIgnitor URL(和其他框架)被分成两半。服务器上存在的真实路径和文件('/login/index.php')和路由+参数('/welcome/main')。 index.php 然后处理路由。 .htaccess 文件是可选的,它只是告诉 Apache 在内部将包含其所在目录的任何 URL 重定向到该 index.php 文件,以便可以将“index.php”完全排除在 URL 之外。 RewiteCond 行列出了不会被重定向的异常。
  • 此外,如果您从 CodeIgniter 目录中添加 HTML 引用的任何其他目录,例如“样式表”或“脚本”,您将希望将它们添加到该异常列表中,以便将它们作为真实文件访问而不是让 Apache 将它们传递给 CodeIgnitor。
【解决方案3】:

您是否设置了.htaccess file?它应该是这样的:

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

【讨论】:

  • 将最后一行设为“RewriteRule ^(.*)$ /login/index.php/$1 [L]”,因为他似乎没有从服务器根目录运行 CI。
  • 我默认在框架中有任何 .htaccess 文件。我不知道如何设置它。将此代码复制到该文件中是否可行?
【解决方案4】:

老兄..

class Welcome extends CI_Controller {

    public function index()
    {
        // echo 'Index';
        $this->load->view('welcome');
        // Then save the file in your application/views/welcome.php
        // http://yours.com/index.php/welcome
        // Or http://yours.com/welcome <-- With htaccess
    }

    public function main()
    {
        // echo 'Index';
        $this->load->view('main');
        // Then save the file in your application/views/main.php
        // http://yours.com/index.php/welcome/main
        // Or http://yours.com/welcome/main <-- With htaccess
    }
}

默认情况下,index.php 文件将包含在您的 URL 中

您可以使用带有一些简单规则的 .htaccess 文件轻松删除此文件。以下是此类文件的示例,使用“否定”方法,其中除了指定项目之外的所有内容都被重定向:

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

【讨论】:

  • 感谢您如此详细的回复。这就是我在应用程序中所做的。我在视图文件夹中有所有视图文件。我提到的 phpinfo 是我在视图页面中作为 phpinfo.php 拥有的文件。至少 en echo 可以工作,不管有没有 index.php。但是除了显示索引函数中的第一页外,什么也没有发生。真正的事实是,即使我没有在 url 中提及函数名称,控制器也应该指向 index 函数,这不会发生。 :(
  • 如果你有一个文件夹来运行你的应用程序,例如:yours.com/folder/welcome/main,你可以把htaccess放在文件夹中
猜你喜欢
  • 1970-01-01
  • 2015-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-03
  • 2013-05-06
相关资源
最近更新 更多