如何在 Codeigniter 上创建 URL 和阅读?
How to create URL & Read on Codeigniter?
如何在 Codeigniter 中创建动态 URL?
我关注了 table,其中存储了基本的文章详细信息:
1. ID
2. Article Title
3. Content
4. Tags
5. Status
6. Create Time
7. Update Time
8. Author ID
9. Status
现在我想知道如何创建动态 URL,其中包含 Page ID
& Article Title
。
例如http://www.domain.com/1/hello-codeigniter
现在上面的示例 URL,我正在用 ID
和 Article Title
生成 URL。
- 检索文章内容的ID(当用户点击文章时,从ID中获取内容)。
- 安全文章标题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>
控制器: 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();
}
}
编码后当我悬停在锚点时 link 我得到这个 URL:
http://localhost/codeIgniter/index.php/content/1/My-First-Blog-Post!
如何从 URL.
中隐藏 index.php
、content
和 1
还有一件事是,当我单击 link 时,我收到此错误消息:
An Error Was Encountered
The URI you submitted has disallowed characters.
如有任何帮助,我们将不胜感激!!!
如果将 slug(安全文章标题 URL)存储在数据库中,则根本不需要公开 ID。不过,这将涉及向您的 table 添加一列。
您可以使用 url_title()
在文章创建时自动生成 slug,详见 here。
<a href="<?php echo base_url()?>index.php/controller_name/method_name/<?php echo $item['name'] ?>/<?php echo $item['id'] ?>" alt="">click here to view</a>
所以你的 URL 看起来像 www.example.com/index.php/controller_name/method/book_name/book id
在控制器中
function method_name($name, $id)
{
echo $name;// this contain book name
echo $id;// this contain book id
//Simply you can use this to get all data
$result = $this->Model_name->get_book_details($id);//in model $id hold the id of book
}
您可以 routes
用于此目的
要删除 index.php
,请使用 .htaccess
和以下代码
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
并在 config.php
$config['uri_protocol'] = 'AUTO';
要从 url 中删除内容,从 视图中的锚
中删除内容
echo anchor($postID.'/'.$urlTitle, $postTitle, array('title' => $postTitle)); // Get post URL
我不认为你应该从 url 中删除 1
。如果您将它从 url 中删除,那么您如何识别您的 post。所以放在url里就好了。或者,如果您想删除,请将此 ID 添加到 post 标题的末尾,如
http://localhost/codeIgniter/My-First-Blog-Post!-1
并展开 id 为 post
的最后一个元素
An Error Was Encountered
The URI you submitted has disallowed characters.
您收到此错误是因为您在 url 中添加了 !
。为了确保您的应用程序安全 CI 防止来自 url 的特殊字符。所以最好不要在 url 中使用特殊字符,除了 _
和 -
.
在你的控制器上
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//';
如何在 Codeigniter 中创建动态 URL?
我关注了 table,其中存储了基本的文章详细信息:
1. ID
2. Article Title
3. Content
4. Tags
5. Status
6. Create Time
7. Update Time
8. Author ID
9. Status
现在我想知道如何创建动态 URL,其中包含 Page ID
& Article Title
。
例如http://www.domain.com/1/hello-codeigniter
现在上面的示例 URL,我正在用 ID
和 Article Title
生成 URL。
- 检索文章内容的ID(当用户点击文章时,从ID中获取内容)。
- 安全文章标题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>
控制器: 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();
}
}
编码后当我悬停在锚点时 link 我得到这个 URL:
http://localhost/codeIgniter/index.php/content/1/My-First-Blog-Post!
如何从 URL.
中隐藏index.php
、content
和 1
还有一件事是,当我单击 link 时,我收到此错误消息:
An Error Was Encountered
The URI you submitted has disallowed characters.
如有任何帮助,我们将不胜感激!!!
如果将 slug(安全文章标题 URL)存储在数据库中,则根本不需要公开 ID。不过,这将涉及向您的 table 添加一列。
您可以使用 url_title()
在文章创建时自动生成 slug,详见 here。
<a href="<?php echo base_url()?>index.php/controller_name/method_name/<?php echo $item['name'] ?>/<?php echo $item['id'] ?>" alt="">click here to view</a>
所以你的 URL 看起来像 www.example.com/index.php/controller_name/method/book_name/book id
在控制器中
function method_name($name, $id)
{
echo $name;// this contain book name
echo $id;// this contain book id
//Simply you can use this to get all data
$result = $this->Model_name->get_book_details($id);//in model $id hold the id of book
}
您可以 routes
用于此目的
要删除 index.php
,请使用 .htaccess
和以下代码
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
并在 config.php
$config['uri_protocol'] = 'AUTO';
要从 url 中删除内容,从 视图中的锚
中删除内容echo anchor($postID.'/'.$urlTitle, $postTitle, array('title' => $postTitle)); // Get post URL
我不认为你应该从 url 中删除 1
。如果您将它从 url 中删除,那么您如何识别您的 post。所以放在url里就好了。或者,如果您想删除,请将此 ID 添加到 post 标题的末尾,如
http://localhost/codeIgniter/My-First-Blog-Post!-1
并展开 id 为 post
的最后一个元素An Error Was Encountered
The URI you submitted has disallowed characters.
您收到此错误是因为您在 url 中添加了 !
。为了确保您的应用程序安全 CI 防止来自 url 的特殊字符。所以最好不要在 url 中使用特殊字符,除了 _
和 -
.
在你的控制器上
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//';