Change the link to a user/view profile

I’m using cakephp 2.9.1
my app have a users and signup
when i want to view any user profile i found the link :
xx.com/users/view/ [id]
and the [id] reefer to the user id in the database
i created another column in the db so whenever some one signup will choose unique username
i want to show the username in the link to the profile
to be
xx.com/users/view/[username]

i searched a lot and did not find the line that directs to view/id

any help ?

by default users/view/XX will call UsersController’s view method and render Users/view template

If you want to change the link itself than in your templates check for them and replace the id to username

can you explain more please ?
what do you mean by templates ? which files in which directory ?
i did check /Users/view.ctp
and did not find anything related to the url

public function view($username = null)
{
   // find user by user name
}
// in template
$this->Html->link($username, ['controller' => 'Users', 'action' => 'view', $username]);
1 Like

the final generated html file is concetaned together from a few (or a lot of) different template files.

By default all controllers renders /src/Template/Layout/default.ctp

In this file you have a <?= $this->fetch('content') ?> line what actually calls the corresponding template file.

By default the url /users/view/XX will be handled by /src/Controller/UsersController.php file’s view method.

If you baked your controller this method requires to pass a user id, and it will find the appropiate user from the database and make the result available for the view (template). If you want to use username instead of id, you should change this method a little bit.

When the controller finished its work it will call /src/Template/Users/view.ctp to display the user.

Your layout and your template files can include other elements (from /src/Template/Element) and these will be also included in the generated HTML document.

Yes, this approach is right.

However for security I would use $this->Html->link(h($username), ['controller' => 'Users', 'action' => 'view', $username]);

so i create public function view($username = null)
in my Usercontroller
and where exactly to put the line for template … under echo $this->fetch(‘content’); ?

This image maybe help you:

52

Yes i’m on that same file but where exactly to put this code
$this->Html->link(h($username), ['controller' => 'Users', 'action' => 'view', $username]);

where do you need it? in navigations or ???

i want it change the url

start with Users/index.ctp,
then learn how to create elements, etc,… Views - 3.10

Change your view function like this:

function view($username = null) {
    if (is_null($username)) {
        $this->cakeError('error404');
    } else {
        $username = $this->User->findByUsername($username);
        $this->set(compact('user'));
    }
}

it keeps giving me this error : Undefined variable username

Hello Ahmed: did you try xx.com/users/view/username ?

Yes
the view file called but all the fields with error line Undefined variable

im missing something. let me back to past. I dont use 2.x from long tome ago. Give me 30 minutes please

are you creating the function view in the controller user?

public function view($username = null) {
    if (is_null($username)) {
        $this->cakeError('error404');
    } else {
        $username = $this->User->findByUsername($username);
        $this->set(compact('user'));
    }
}

this one, yes

this example worked fine for me.

http://linuxformat.com/tuxradarchive/content/cakephp-tutorial-data-storage-baking-and-slugs