如何在 codeigniter 中制作永久链接......社会案例

发布于 2024-12-23 06:30:48 字数 517 浏览 1 评论 0原文

我想使用 codeigniter 建立一个网站。该网站看起来就像我学校的社交聚合器。为了建立我的网站,我计划:

  1. 制作一个类“页面”。 “pages”类有一个常用的功能login,
    注册,注销......等
  2. 创建一个“用户”类,“用户”类具有与以下相关的功能 用户需求,例如:编辑个人资料、添加社交 api、view_profile 等。

我知道如果我们想查看个人资料,我们应该传递一个网址,例如:

www.Mysite.com/user/view_profile/ <user name>

我不知道如何制作直接用户页面(例如永久链接) )。我希望我的用户只需输入即可访问他的页面:

www.Mysite.com/ <user name>

我已阅读代码点火器中的 user_guide 但我仍然不明白 url 类别。有谁可以解释一下如何制作吗?

i want to build a website using codeigniter. The website is look like a social agregator for my school. to build my website i plan :

  1. making a class "pages". The class "pages" has a common function login,
    register , logout.. etc.
  2. making a "user" class the class "user" has a function related to
    user needs like: edit profile, add social api, view_profile etc.

i know if we want to see a profile we should pass an url like :

www.Mysite.com/user/view_profile/ <user name>

I dont know how to make a direct user pages (like permalink). i want my user can access his pages just only to type:

www.Mysite.com/ <user name>

i have read the user_guide in code igniter but i still dont understand what the url clas. is there any body can explain me how to make it ?

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

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

发布评论

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

评论(1

许你一世情深 2024-12-30 06:30:48

我将在 application/config/routes.php 中设置一条路由,将任何以用户名作为第一段的 URL 重新映射到为您的个人资料视图提供服务的控制器方法。

例如,在您的routes.php中放置以下代码:

$route[':any'] = "user/view_profile/:any";

:any 键将为作为变量传递给函数。请记住,默认情况下,该路由中的任何内容(任何内容)都将被路由到该控制器的方法,因此让您的永久链接结构如下所示可能是一个好主意: yoursite.com/u/yoursite.com/u/< username>,在这种情况下你不需要路由;你可以像这样传递 uri 段:

<?php
    class U extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();
            // Load the users model
            $this->load->model('users_model');                
        }

        function index()
        {
            // Get the username from the URL
            $username = $this->uri->segment(2);

            // Get the users data from the database using the second URI segment
            $data['user'] = $this->users_model->get_user($username);

            // Load the view
            $this->load->view('path/to/view', $data);
        }
    }

I would set up a route in application/config/routes.php that remaps any URL with a username as the first segment to the controller's method serving your profile view.

For instance, in your routes.php place this code:

$route[':any'] = "user/view_profile/:any";

The :any key will be passed as a variable to the function. Keep in mind that, by default, anything in that route (anything) will be routed to that controller's method, so it might be a good idea to have your permalink structure look like this: yoursite.com/u/<username>, in which case you don't need a route; you can just pass the uri segment like this:

<?php
    class U extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();
            // Load the users model
            $this->load->model('users_model');                
        }

        function index()
        {
            // Get the username from the URL
            $username = $this->uri->segment(2);

            // Get the users data from the database using the second URI segment
            $data['user'] = $this->users_model->get_user($username);

            // Load the view
            $this->load->view('path/to/view', $data);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文