• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with virtual directories

#1
[eluser]cmgmyr[/eluser]
Hello all,
Hopefully this will be a quick answer but I couldn't find it anywhere. I would like to do something like this:

domain.com/{username} (controller 1)
domain.com/{username}/controller2
domain.com/{username}/controller3

Example:
domain.com/cmgmyr (profile page)
domain.com/cmgmyr/friends
domain.com/cmgmyr/friends/add
domain.com/cmgmyr/music
domain.com/cmgmyr/music/vote

How do I set it up so that I can pass the user name to a function, get the userid, then pass that to the other controllers/functions?

Thanks in advance.
-Chris

#2
[eluser]drewbee[/eluser]
I would actually do it a little bit differently. The way you are requiring is that the username (any) is the controller. You could do it with routing, but it leaves a little to much.... open if you ask me.

I havn't tested this, so you will need to adjust it properly. But I would imagine something along these lines coupled with the right routing rules you could do the following:
domain.com/user/cmgmyr/
domain.com/user/cmgmyr/friends/
domain.com/user/cmgmyr/music/
domain.com/user/cmgmyr/music/vote
Code:
controller User extends Controller()
{
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
      // Profile page
    }

    function friends($username = '')
    {
        $friendsModel->getFriendsFromUsername($username);
    }

    function music($username = '', $vote = '')
    {
        $friendsModel->getMusicFromUsername($username);
    }
}

//Then with routing... you will have to switch it around so that the variable is actually the controller and //the controller is the variable
$route['user/(:any)/friends'] = "user/friends/$1";
$route['user/(:any)/music'] = "user/music/$1";
$route['user/(:any)/music/vote'] = "user/music/$1/vote";
$route['user/(:any)'] = "user/index/$1";

I hope this gives you the general idea of what your looking for.

#3
[eluser]cmgmyr[/eluser]
Thanks for the reply. I see where that would be a lot easier to do, but the problem is with the seo and current structure of the site I would have to do that as a last resort.

Also I would need to make sure I can still have other pages to the site also. Like: domain.com/about, domain.com/contact

Thanks!
-Chris

#4
[eluser]drewbee[/eluser]
Other pages would not be a problem with that. Those are simply more controllers.

Code:
class About extends Controller()
{
    function index()
    {

    }
}

class Contact extends Controller()
{
    function index()
    {

    }
}

Im not sure what you mean by SEO either. You can't much more SEO friendly then what code igniter gives out of the box!

#5
[eluser]cmgmyr[/eluser]
Thanks again for the reply. Sorry about the SEO I should have been more clear. I don't want to lose the current site setup. The current site is domain.com/cmgmyr and I would like to convert it to CI, but I don't want to lose what I already have up on the web. Make more sense?

Thanks!
-Chris

#6
[eluser]ehicks727[/eluser]
[quote author="cmgmyr" date="1219386500"]The current site is domain.com/cmgmyr and I would like to convert it to CI, but I don't want to lose what I already have up on the web. [/quote]

Just 301 redirect your pages in your .htaccess file. You won't lose anything. I've redirected thousands of pages this way, necessary because of a change in strategy or planning, and you don't lose your 'Google juice', so to speak.

#7
[eluser]@li[/eluser]
Just setup routes for all the static pages like About etc to the controller, then do a /:any/ route to the user controller. In the user controller itself you can validate the username being passed, also CodeIgniter disallows any characters other than a-z0-9-_ etc in the url, so I don't see any security issues with this at all.

#8
[eluser]cmgmyr[/eluser]
Thanks @li, that sounds like it would work out. There are only a few static pages so this wouldn't be too much work. Thanks again for the help!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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