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

#11
[eluser]benners[/eluser]
If I have this as my routes.php

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


And this _remap
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();
  }
}

User URL works but all other URLS give 404.

------

If I try this routes.php
$route['default_controller'] = 'login';

And this _remap

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();
   }
  }
}
All URLs work except user which gives 404.

I seem to be going round in circles. Please put me out of my misery!

#12
[eluser]CroNiX[/eluser]
[quote author="benners" date="1330461269"]If I have this as my routes.php
User URL works but all other URLS give 404.
[/quote]
What other URL's aren't working? Are they in different controllers?

#13
[eluser]benners[/eluser]
URLs for methods in the same controller and all other controllers are giving 404.
404 Examples:
mysite/login/validate
mysite/contact
etc

OK.
mysite/valid_user

#14
[eluser]CroNiX[/eluser]
It's because of your single catch-all route. It's telling every request to go to the login controller. And, since you are using _remap() in that controller, everything goes there. You need to set additional routes up now for your other controllers, before your catch-all route, and adjust your _remap() (you never mentioned you had other methods in there that needed to be accessed).

Code:
$route['contact'] = 'contact'; //will go to contact/index

Code:
public function _remap($method, $params = array())
{
  if (method_exists($this, $method))  //this should work for your 'validate()' method
  {
    echo $method;
    print_r($params);
    return call_user_func_array(array($this, $method), $params);
  }
  else
  {
    // actual method doesn't exist, check if request was for a user
    $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
    {
      //user didn't exist
      show_404();
    }
  }
}
Edit: Remember, once you start using routes, you need to route everything because you are straying away from CI's default setup of requests containing controller/method. Routes go in order from top to bottom. As SOON as a route matches, it will execute that route and ignore anything after. So if you only have 1 route that is a catch-all, everything will go there unless you have a route that matches before the catch-all.

#15
[eluser]benners[/eluser]
$route['default_controller'] = 'login';
$route['contact'] = 'contact';
$route['(.*)'] = 'login/$1';

Adding route Contact has sorted requests to mysite/contact and user check works, but all other URLs don't work. I haven't listed them all but here's a few more.
mysite/login/signup
mysite/another_page

Do I need to add a route for each Controller I create?

#16
[eluser]CroNiX[/eluser]
Yes you need to create a route for all controllers as I mentioned in my edit (if you choose to use routes).

For the 'login/signup' route problem...are you using links to access these URLs? If so, it should just point to 'yoursite.com/signup' and then your catchall route will pick it up and send it to the _remap() method, and since that method (signup) will exist it will send it there.

Edit: one note about this setup, if a user is named 'signup' you will run into problems...since you have a method with that name. Unlikely to happen, but still something to consider.

#17
[eluser]benners[/eluser]
Thanks for your help. Will do a bit more testing tomorrow. Sorry if it's been painful!

Quote:(if you choose to use routes).
Is there another (better) way of acheiving what I'm after?

Thanks for the tip re controller name conflicts. During signup I check the DB for usernames and also a config array of controllers .

#18
[eluser]benners[/eluser]
Done some more testing and it seems it will probably work with the following setup but it means I have to put 2 routes for each controller I have. Isn't there a better way that this? It's not exactly automated.

Main controller - main.php
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();
  }
}
}

routes.php
Code:
$route['default_controller'] = 'main';
$route['login/(:any)'] = 'login/$1';
$route['login'] = 'login';
$route['contact'] = 'contact';
... // All my other pages
$route['(.*)'] = 'main/$1'; // sends all other requests to main.php for remapping


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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