How to make permalink in codeigniter… social case

2019-06-12 01:08发布

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条回答
Evening l夕情丶
2楼-- · 2019-06-12 01:19

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);
        }
    }
查看更多
登录 后发表回答