【问题标题】:Can't access to the page无法访问页面
【发布时间】:2016-06-04 12:21:23
【问题描述】:

我是 Codeignator 的新手。当我浏览它时,它会向我显示“404 页面未找到”之类的错误。下面是代码

控制器代码: 插入.php

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

class Insertion extends CI_Controller {


    public function Insert()
    {
        $this->load->view('Insert_view');
    }
}
?>

查看代码:Insert_view.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Welcome to CodeIgniter</title>

</head>
<body>

<div id="container">
    <h1>Hello world!</h1>
</div>

</body>
</html>

【问题讨论】:

  • 将控制器文件名设为Insertion.php 而不是Insert.php
  • 输入了什么网址?

标签: php html codeigniter


【解决方案1】:

如果您的 url 类似于 www.mywebsite.com/insertion,那么您的控制器文件应该是带有 Insertion 类的 Insertion.php

如果您喜欢使用 www.mywebsite.com/insert,那么您的控制器文件应该是带有 Insert 类的 Insert.php,并将 Insert 函数更改为 index

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

 class Insertion extends CI_Controller {

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

 }

?>

【讨论】:

    【解决方案2】:

    第一个查看文件代码命名(Insertion_view.php)

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Welcome to CodeIgniter</title>
    </head>
    <body>
    <div id="container">
        <h1>Hello world!</h1>
    </div>
    </body>
    </html>
    

    然后是 Controller 文件 代码命名为 (Welcome.php)

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Welcome extends CI_Model{
    public function Insert()
        {
            $this->load->view('Insertion_view');
        }
    

    现在在浏览器中写入这个 URL http://127.0.0.1/Welcome/insert

    这里insert是来自Welcome.php的方法调用并显示Insertion_view.php

    你会得到 **


    你好世界!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多