• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to display a users personal page from URL - mysite/username

#1
[eluser]benners[/eluser]
I want to give registered users a personal public (not requiring login) page. E.g. mysite.com/username

I'm new to PHP and CI and I'm not sure how to about this. So far I have this but I get a 404 if I test mysite.com/username. I'm wondering if I need to do a .htaccess redirect if request is not in a list of controller names.

Code:
class MY_Controller extends CI_Controller
{
public function __construct()
{
  parent::__construct();
  $this->is_user_page();
}

function is_user_page()
{
  $username = $this->uri->segment(1);
  
  $query = $this->db->query('SELECT * FROM membership where username="'.$username.'"');
  if($query->num_rows() > 0)
   $data['user_page'] = $username;
   $data['main_content'] = 'user';
   $this->load->view('includes/template', $data);

}

#2
[eluser]InsiteFX[/eluser]
You need to do it in the main controller index method!

Main Controller:
Code:
// --------------------------------------------------------------------

/**
* index()
*
* index - default method called.
*
* @access public
* @return void
*/
public function index($user_id = NULL)
{
    if ( ! empty($user_id))
    {
        // Do your stuff for the user with this id.
        // like redirect to the users profile page.
    }

    // The rest of your code here for normal stuff.
}

// --------------------------------------------------------------------

/**
* _remap()
*
* Remaps the URL segments.
*
* @access private
* @param string
* @param array
* @return mixed
*/
public function _remap($method, $params = array())
{
    if (method_exists($this, $method))
    {
        return call_user_func_array(array($this, $method), $params);
    }

    show_404();
}

Add this to ./application/config/routes.php
Code:
// ------------------------------------------------------------------------
/**
* DO NOT! REMOVE THIS LINE - Make sure it is the last line in the routes file!
* This is for the users_id - _remap() Method.
* ------------------------------------------------------------------------
*/

// Chage the controller name to your controller.
$route['(.*)'] = 'welcome/index/$1';

// --------- -== DO NOT REMOVE! the above line ==- ------

#3
[eluser]benners[/eluser]
Thanks, that looks ideal.

Can you tell me what I should have in regards to routing as I've removed the index.php?

Currently I have:

routes.php
Code:
$route['default_controller'] = "login";

web.config
Code:
<rule name="Rewrite CI Index">
<match url="^(.*)$" />
<conditions>
  <add input="{REQUEST_FILENAME}" pattern="css|js|jpg|jpeg|png|gif|ico|htm|html|php" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:0}" />
</rule>


#4
[eluser]benners[/eluser]
I've made some progress and it's nearly working ok. I've left the URL rewriting in the web.config to remove the index.php and my routes.php is

$route['default_controller'] = 'login';
$route['(.*)'] = 'login/$1';

mydomain/valid_user loads the correct view, but mydomain/invalid_user shows the index() instead of 404. Also mydomain/existing_function shows the index() instead of the valid function. Any ideas? Thanks.

Code:
public function _remap($user)
{
$user_info = $this->membership_model->get_member($user);
if(count($user_info) > 0){
// Display the user with the profile page.
$data['user_info'] = $user_info;
$data['main_content'] = 'profile_view';
$this->load->view('includes/template', $data);  
}
else{
$this->index();
}

function index() {
}

function existing_function() {
}

#5
[eluser]CroNiX[/eluser]
If you don't want the index() method used for invalid users, why are you telling it to do exactly that? If the user info exists, you tell it to load a view, if it doesn't you tell it to go to $this->index().

If that is a copy/paste of your actual code, your other problem could be due to a missing } in your _remap() method. If you follow the CI Style Guide, that sort of error is very easy to spot as opening braces should align with closing braces, except for class declarations.

#6
[eluser]benners[/eluser]
Thanks for your comments. I've found the following seems to work.

Code:
public function _remap($method, $params = array())
{
  if (method_exists($this, $method)) {
   return call_user_func_array(array($this, $method), $params);
  } else {
   $user_info = $this->membership_model->get_member($method);
   if(count($user_info) > 0){
    // Display the user with the profile page.
    $data['user_info'] = $user_info;
    $data['main_content'] = 'profile_view';
    $this->load->view('includes/template', $data);  
   } else {
    show_404();
   }
  }

#7
[eluser]CroNiX[/eluser]
It seems you are still missing a closing }. Count the { and }, they should be the same quantity. I count 5 opening braces and 4 closing.

#8
[eluser]benners[/eluser]
Just a copy and paste error. Thanks

#9
[eluser]benners[/eluser]
I spoke to soon. The code below is not actually running the correct methods. The method_exists keeps running index(). Will method_exists do what I need it to?
Code:
public function _remap($method, $params = array())
{
  if (method_exists($this, $method)) {
   echo $method;
   print_r($params);
   return call_user_func_array(array($this, $method), $params);
  } else {
   $user_info = $this->membership_model->get_member($method);
   if(count($user_info) > 0){
    // Display the user with the profile page.
    $data['user_info'] = $user_info;
    $data['main_content'] = 'profile_view';
    $this->load->view('includes/template', $data);  
   } else {
    show_404();
   }
  }
}

#10
[eluser]CroNiX[/eluser]
I believe your route is telling it to go to the index method, so it will.
Try: $route['(.*)'] = 'welcome/$1';
You don't want to use method exists with the way you are using _remap(). If you make the above change to your route, $method will actually be the user name, which won't exist as a method.
Code:
public function _remap($method, $params = array())
{
  $user_info = $this->membership_model->get_member($method);
  if (count($user_info) > 0)
  {
    // Display the user with the profile page.
    $data['user_info'] = $user_info;
    $data['main_content'] = 'profile_view';
    $this->load->view('includes/template', $data);  
  }
  else
  {
    show_404();
  }
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.