How to make permalink in codeigniter… social case

柔情痞子 提交于 2019-12-08 12:45:42

问题


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 ?


回答1:


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);
        }
    }


来源:https://stackoverflow.com/questions/8653160/how-to-make-permalink-in-codeigniter-social-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!