过滤字符串以删除不允许的字符以在 CodeIgniter 中组成 URL

发布于 2024-09-27 23:58:22 字数 318 浏览 0 评论 0原文

我正在尝试为我的作品集上的博客创建 URL 友好的链接。 所以我想获得类似 site/journal/post/{title} 的链接

显然 Journal 是我的控制器,但假设我的标题是“mysite.com 上线!”我想要一个有效的网址,例如 site/journal/post/mysitecom-goes-live,其中所有不允许的字符都被删除。

我将如何改造“mysite.com 上线!”根据 $config['permissed_uri_chars'] 中的字符在 CodeIgniter 中转换为“site/journal/post/mysitecom-goes-live”

I'm trying to make URL-friendly links for the blog on my portfolio.
So I would like to obtain links something like site/journal/post/{title}

Obviously Journal is my controller, but let's say my title would be 'mysite.com goes live!' I would like to have a valid url like site/journal/post/mysitecom-goes-live where all disallowed characters are removed.

How would I transform 'mysite.com goes live!' to 'site/journal/post/mysitecom-goes-live' in CodeIgniter based on the characters in $config['permitted_uri_chars']

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

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

发布评论

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

评论(1

风吹雨成花 2024-10-04 23:58:22

使用 url 帮助器

$this->load->helper('url');

$blog_slug = url_title('Mysite.com Goes live!');

echo $blog_slug //mysitecom-site-goes-live 
// might differ slightly, but it'll do what you want.

生成 url 友好的链接。

将此值存储在博客表 (url_title/url_slug) 的字段中。

创建一个函数:

class Journal extends controller
{
   //make your index/constructor etc

   function view($post)
   {
     $this->blog_model->get_post($post);
     // etc - your model returns the correct post,
     // then process that data and pass it to your view
   }
}

您的 blog_model 有一个 get_post 方法,该方法使用 CI 的

$this->db->where('url_title', $post);

希望这是有意义的。

然后,当您访问该页面时:

site.com/journal/view/mysite-goes-live

该函数将拾取“mysite-goes-live”并将其传递给 view() 函数,依次在数据库中查找适当的博客条目。

use the url helper

$this->load->helper('url');

$blog_slug = url_title('Mysite.com Goes live!');

echo $blog_slug //mysitecom-site-goes-live 
// might differ slightly, but it'll do what you want.

to generate url-friendly links.

Store this value in a field in your blog table (url_title/url_slug) whatever.

make a function:

class Journal extends controller
{
   //make your index/constructor etc

   function view($post)
   {
     $this->blog_model->get_post($post);
     // etc - your model returns the correct post,
     // then process that data and pass it to your view
   }
}

your blog_model has a method get_post that uses CI's

$this->db->where('url_title', $post);

hope that makes sense.

then when you access the page:

site.com/journal/view/mysite-goes-live

the function will pick up "mysite-goes-live" and pass it to the view() function, which in turn looks up the appropriate blog entry in the database.

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