Sarjil Shaikh
Sarjil Shaikh

Reputation: 21

how to get name of dynamically generated method in codeigniter

I have achieved url as follows

myapp.com/profile/username1

myapp.com/profile/username2

myapp.com/profile/username3

my routes.php file contains following line of code

$route['profile/(:any)'] = 'profile/user_profile/$profile_method';

my profile controller contains this code

    public function user_profile($profile_method)
    {
        echo $profile_method;
    }

all is well but the problem is i want to return username to process further, this method returns the parameter name instead of username. please tell me where i am doing wrong ?

Upvotes: 0

Views: 673

Answers (1)

user4419336
user4419336

Reputation:

Just tip You have no / after username myapp.com/profile/username/1

Try

$route['profile/(:any)'] = 'profile/user_profile/$1';
$route['profile/(:any)/(:num)'] = 'profile/user_profile/$1/$2';

Instead of

$route['profile/(:any)'] = 'profile/user_profile/$profile_method';

http://www.codeigniter.com/user_guide/general/routing.html#examples

Then

public function user_profile($profile_method = '', $id = '')
{
    echo $profile_method . ' <br/> ' . $id;
}

Make sure you have named your controllers and other files correct where only the first letter of class and file name is upper case Profile.php

You may need to have a htaccess in main directory to remove index.php from url also.

https://github.com/wolfgang1983/htaccess_for_codeigniter


From Magnus Eriksson comment

CI is using regular expressions to replace the dynamic url variable, which means that $1 = the first match, $2 = the second match etc. The $-part isn't am ordinary php-variable, so you can't name it to what you want.


Upvotes: 3

Related Questions