php - CI Base URL Routing -


For example, in Twitter, you can get this URL format:

"Username" With user name is for user

I'm thinking properly in Codeigniter. I'll need the same format. I have other pages like user account management, etc. Do I need this route through a function, check that the user has it and then pass it to another controller? Thanks!

Extend the router class by placing the MY_Router.php in your code Application \ libraries directory and use this code:

  & lt ;? Expanding Php class MY_Router CI_Router {function_validate_request ($ Segment)} // Is Requested Controller Root Folder? If (file_exists (APPPATH. 'Controllers /'. $ Section [0] .EXT)) {return $ segment; } // Is the subfolder in the controller? If (is_dir (APPPATH.'controllers/'.$ Segment [0])) {// Set the directory and remove it from the Segment array - $ this- & gt; Set_directory ($ Segment [0]); $ Block = array_slice ($ Segment, 1); If (count ($ Segment)> gt; {// Does the subcontractor have the requested controller present? If (! File_exists (APPPATH.'controllers/'.$ this-> fetch_directory (). $ Segment [0] .EXT)) {show_404 ($ this- & gt; fetch_directory (). $ Segment [0]) ; }} And {$ this-> Set-class ($ this- & gt; default_controller); $ This- & gt; Set_method ('index'); // Does the default controller exist in subfolders? If (! File_exists (APPPATH.'controllers/'.$ this-> fetch_directory (). $ This- & gt; default_controller.EXT)) {$ this- & gt; Directory = ''; Return array (); }} Return $ Segment; } // // // This is the new code below // ** // divides this section into its known class (user) & amp; The method for all the controller calls (index) // which does not exist as a file / or $ my_segments = array in the directory ('user', 'index', $ segment [0]); $ My_segments return; }}  

Now, simply create a user controller with an index method that accepts the user as the first parameter:

< Pre> & lt ;; Php Class User Controller {Function Index ($ username = '') {// validate HECK from outside the usernames / Confirm HECK exits from $ username. Go out(); }}

This is a ballin 'answer! CI tested at 1.7.2. Not aware of 2.0, though ...


Comments