How to display a users personal page from URL - mysite/username - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: How to display a users personal page from URL - mysite/username (/showthread.php?tid=49669) Pages:
1
2
|
How to display a users personal page from URL - mysite/username - El Forum - 02-28-2012 [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 How to display a users personal page from URL - mysite/username - El Forum - 02-28-2012 [eluser]InsiteFX[/eluser] You need to do it in the main controller index method! Main Controller: Code: // -------------------------------------------------------------------- Add this to ./application/config/routes.php Code: // ------------------------------------------------------------------------ How to display a users personal page from URL - mysite/username - El Forum - 02-28-2012 [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"> How to display a users personal page from URL - mysite/username - El Forum - 02-28-2012 [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) How to display a users personal page from URL - mysite/username - El Forum - 02-28-2012 [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. How to display a users personal page from URL - mysite/username - El Forum - 02-28-2012 [eluser]benners[/eluser] Thanks for your comments. I've found the following seems to work. Code: public function _remap($method, $params = array()) How to display a users personal page from URL - mysite/username - El Forum - 02-28-2012 [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. How to display a users personal page from URL - mysite/username - El Forum - 02-28-2012 [eluser]benners[/eluser] Just a copy and paste error. Thanks How to display a users personal page from URL - mysite/username - El Forum - 02-28-2012 [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()) How to display a users personal page from URL - mysite/username - El Forum - 02-28-2012 [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()) |