0

I want to generate a url like ../profile/firstname.lastname

But i don't understand how i can do this because i want in my php also the id from the user.

this is my controller:

       $controllers
            ->match('profile/{firstname.lastname}/', array($this, 'profile'))
            ->assert('userId', '\d+')
            ->method('GET|POST')
            ->before(array($this, 'checkLogin'))
            ->bind('home.profile');
3
  • Could you provide a bit more code to help explain the problem you're trying to solve? Where is this code being called? Commented May 16, 2014 at 15:13
  • I don't have more code because i don't know how to do. Now i can pass the id through the url like profile/1 but i want to pass the names. Commented May 16, 2014 at 15:16
  • Where did you put that code and what exactly do you want to achieve. Please describe the problem more detailed. You want to generate a URL in a controller-action to ... ? What is that $controllers variable in your code example - the @router service ... or what? Commented May 16, 2014 at 16:06

2 Answers 2

1

You have to build your Route like this

<route id="profile" path="/profile/{first_name}.{last_name}">
        <default key="_controller">...</default>
    </route>

And in controller create like this

$this->generateUrl('profile', [ 'first_name' => 'skowron', 'last_name' => 'line' ]);

but if you have 2 profiles with this same data it will give you bad results. Then you can create slug or add id parameter to your route

Sign up to request clarification or add additional context in comments.

Comments

0

Symfony uses Symfony\Component\Routing\Generator\UrlGenerator::generate() to generate paths. But probably the path is not defined in your routing system. So you must create an instance of it and pass a RouteCollection which only contains tha path you want and it does not seem to be a good method.

It's better to use preg_replace.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.