php - 如何在 Codeigniter 上创建 URL 和阅读?

标签 php codeigniter codeigniter-2 codeigniter-routing

如何在 Codeigniter 中创建动态 URL?

我遵循存储基本文章详细信息的:

 1. ID
 2. Article Title
 3. Content
 4. Tags
 5. Status
 6. Create Time
 7. Update Time
 8. Author ID
 9. Status

现在我想知道如何创建动态 URL,其中包含 Page IDArticle Title

例如http://www.domain.com/1/hello-codeigniter

现在在示例 URL 上方,我正在生成带有 ID文章标题 的 URL。

  1. 用于检索文章内容的ID(当用户点击文章时,从ID中获取内容)。
  2. 安全 URL 的文章标题。

但我不想显示来自 URL 的 ID 并在用户重定向到详细信息页面时仍然获取内容。

这是我的代码:

查看: home_page.php

<div id="body">
        <?php for($i=0; $i<count($blogPosts); $i++): ?>
            <div class="post-preview">
                <?php
                    $postID = $blogPosts[$i]->id; // Get post ID
                    $postTitle = $blogPosts[$i]->title; // Get post Title
                    $urlTitle = preg_replace('/\s+/', '-', $postTitle); // Replacing space with dash
                    echo anchor('content/'.$postID.'/'.$urlTitle, $postTitle, array('title' => $postTitle)); // Get post URL
                    echo "<br />";
                ?>
            </div>
            <hr>
        <?php endfor; ?>
    </div>

Controller : home.php

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

class Home extends CI_Controller {

    public function index() {

        $this->load->model("HomeModel"); // Load HomeModel
        $data['blogPosts'] = $this->HomeModel->blogPosts(); // Get blog posts data from blogPostsData function
        $this->load->view("home_page", $data);

    }

}

模型: HomeModel.php

class HomeModel extends CI_Model {

    public function blogPosts() {

        $this->db->select('*');
        $this->db->from('blog_posts');
        $query = $this->db->get();
        return $query->result();

    }

}

编码后当我将鼠标悬停在 anchor 链接上时,我得到了这个 URL:

http://localhost/codeIgniter/index.php/content/1/My-First-Blog-Post!

如何从 URL 中隐藏 index.phpcontent1

还有一件事是,当我点击链接时,我收到了这个错误信息:

An Error Was Encountered 
The URI you submitted has disallowed characters.

任何帮助将不胜感激!!!

最佳答案

您可以为此目的使用路由

要删除 index.php,请使用 .htaccess 和以下代码

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

并在 config.php

$config['uri_protocol'] = 'AUTO';

要从 url 中删除内容,请从 in view 的 anchor 中删除内容

echo anchor($postID.'/'.$urlTitle, $postTitle, array('title' => $postTitle)); // Get post URL

我不认为你应该从 url 中删除 1。如果你从 url 中删除它,那么你如何识别你的帖子。所以最好将它保存在 url 中。或者,如果您想删除,请在帖子标题的末尾附加此 ID,例如

http://localhost/codeIgniter/My-First-Blog-Post!-1

并分解帖子 id 的最后一个元素

An Error Was Encountered 
The URI you submitted has disallowed characters.

您收到此错误是因为您在 url 中添加了 !。使您的应用程序安全 CI 防止来自 url 的特殊字符。因此,除了 _- 之外,最好不要在 url 中使用特殊字符。

在你的 Controller 上

class Content extends CI_Controller {
    function index($id, $name)
    {
        echo $name;// this contain book name 
        echo $id;// this contain book id

        //your code
    }
}

现在在你的路线 application/config/routes.php

$route['(:num)/(:any)']='content/index/$1/$2';

关于php - 如何在 Codeigniter 上创建 URL 和阅读?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31769335/

相关文章:

upload - 出于安全原因,escapeshellarg() 已被禁用

php - CSV 导入以逗号分隔 - 如何处理引号?

php - Codeigniter 根据 View 中的 id 在 View 中显示来自 2 个不同表的数据

php - mysql更新时出错

javascript - 当结果 <= 限制 CI 时隐藏加载更多按钮

使用codeigniter进行MySql查询

php - IIS 上带有 web.config 文件的 Codeigniter 2

php - 如何从目录和子目录中找到包含在多个文件中的函数?

php - Codeigniter - 如果选项不同,则添加具有相同 ID 的产品

php - Mysql跨表查找