0

I have been tasked to explore the possibilities of creating dynamic urls. What we try to achieve is serving a domain for the client.

eg. www.example.com/clients/john/

eg. www.example.com/clients/bill/

These urls are not fixed, but have to be made dynamically everytime a Client registers at our site. Our URL's are build of Controllers and Actions. Like www.example.com/controller/action.

How can this be achieved in the PHP Zend Framework 2?

Big thanks in advance!

0

1 Answer 1

5

All you need is the ZF2 Manual:

I suggest to use segment routes You can define them like:

 'client' => array(
    'route' => '/clients/:username',
    'constraints' => array(
        'username' => '[a-zA-Z][a-zA-Z0-9-]+', //accepted value pattern
    ),
    'defaults' => array( //which action to invoke
        'controller' => 'Application\Controller\ClientController',
        'action'     => 'showclient',
    ),
));

Then you can create an url with url helper like:

$this->url('client', array('username' => 'John'));

And in your controller you will be able to get username param with:

$this->params()->fromRoute('username');
Sign up to request clarification or add additional context in comments.

1 Comment

This is definitely a better approach than the above regex answer.

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.