Welcome Guest, Not a member yet? Register   Sign In
custom url to username and maintain urls for controllers
#1

[eluser]asumaran[/eluser]
I want to assign a vanity url (like facebook) to any registered user in my app just like:
Code:
site.com/username
I tried putting this in conf/routes.php:
Code:
$route['([a-zA-Z0-9_-]+)'] = "user/profile/$1";
this works very well, but when I put this:
Code:
site.com/controller
if 'username'='controller', this display "user/profile/controller" and this is not what I want.

I tried putting adding more routes in routes.php like this:
Code:
/* Exceptions */
$route['tv'] = "tv";
$route['radio'] = "radio";
$route['media'] = "media";
//...and 20 controllers more

$route['([a-zA-Z0-9_-]+)'] = "user/profile/$1";
$route['([a-zA-Z0-9_-]+)/blog'] = "blog/view/$1";

this needs more rules for every controller.
this is not elegant way to resolve it,

other way would be assign to user a URL like
site.com/users/username
yes, but I like the short way Smile


PS.
-- sorry my english ;-)
#2

[eluser]Dam1an[/eluser]
Take a look at thus router extension

If the first segment maps to a controller, it will execute the controller as normal, otherwise, it will default to a fallback controller

I use it for basic CMS functionaity in my sites
#3

[eluser]asumaran[/eluser]
thanks! it seems that is what I need
#4

[eluser]asumaran[/eluser]
I had solved my problem this way, maybe it isn't the best way, but it works

config/routes.php
Code:
$url_parts = explode('/',$_SERVER['REQUEST_URI']);
$reserved_routes = array("tv", "radio", "media", "students","other_reserved_controller_name");

if (!in_array($url_parts[1], $reserved_routes)) {    
    $route['([a-zA-Z0-9_-]+)'] = "profile/view/$1";
    $route['([a-zA-Z0-9_-]+)/blog'] = "blog/view/$1";
    $route['([a-zA-Z0-9_-]+)/blog/([a-zA-Z0-9_-]+)-:num'] = "blog/view_post/$2";    
}

I will try the other way ;-)
#5

[eluser]Unknown[/eluser]
^how is this approach different or better than just specifically defining the reserved controller routes like this:
$route['tv'] = "tv";
$route['radio'] = "radio";
$route['students'] = "students";
$route['media'] = "media";

is there anything wrong with my approach?
i am curious... as i want to implement it and want to know the best possible way.
#6

[eluser]philpalmieri[/eluser]
HI, my approach was similar http://philpalmieri.com/2010/04/personalized-user-vanity-urls-in-codeigniter/
#7

[eluser]Unknown[/eluser]
Hi, here is a simple way...

$route['404_override'] = 'user/profile'; // Just add your controller/function in case controller not found Wink

Apply a condition while creation of account or blog so your code should check all controller's file name if any controller exists then alert user that this username is not available.. Wink

Thanks

Regards
#8

[eluser]InsiteFX[/eluser]
Posting to a 3 year old topic?

Plus there is a better way to do it, welcome controller:
Code:
public function index($username = NULL)
{
        if ( ! empty($username))
        {
            // username has been passed in...
        }
        else
        {
            // default index action...
        }

}

public function _remap($method, $params = array())
{
    if (method_exists($this, $method))
    {
        return call_user_func_array(array($this, $method), $params);
    }

    show_404();
}

// ./application/config/routes.php

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

/**
* DO NOT! REMOVE THIS LINE.
* This is for the index _remap.
* ------------------------------------------------------------------------
*/
$route['(.*)'] = 'welcome/index/$1';

// DO NOT REMOVE! the above line
#9

[eluser]Aken[/eluser]
That way isn't better, because then every single one of your controller methods needs to be in that welcome controller. You wouldn't be able to use other controllers without adding them to the routes.

The 404 controller is the easiest way to use CI controllers like normal, and then a catch-all wildcard controller for things like usernames. CI isn't really set up to handle first-segment wildcards properly, which will probably be addressed in the future. For the time being, it's the best solution.




Theme © iAndrew 2016 - Forum software by © MyBB