Gnagy
Gnagy

Reputation: 1462

PHP ZF2 - How to create dynamic urls

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!

Upvotes: 0

Views: 333

Answers (1)

Jakub Matczak
Jakub Matczak

Reputation: 15656

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');

Upvotes: 5

Related Questions