I'm working on Zend Framework and creating a profile based website. I got stuck when I suppose to create profile url. Requirement is like:
http[:]//abc.com/myprofileurl
I'm new to zend and I just know that in the zend framework, url rule is
http[:]//domain.com/controller/action/params
But I need to put profile name in the place of controller.
So, guys please help me to solve this problem. I already spent 4 hours in searching solution over the internet but can't find anything.
Thanks in Advance
I got the solution by experimenting my self.
I've created a module named "User" and then created a controller named "Profile" and add a routes in module.config.php inside User module as below:
'router' => array(
'routes' => array(
// other routes configs here
'profile' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:username',
'constraints' => array(
'username' => '[\w-.]+',
),
'defaults' => array(
'__NAMESPACE__' => 'User\Controller',
'controller' => 'Profile',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
),
),
),
),
This works exactly what I want :P
http[:]//mydomain.com/myusername
Thanks guys!!!
I advice you to add some discriminating values to your url to avoid collision of rules. In the example I'm going to show all 'user' related urls are under the prefix 'user', so we can have user/login, user/logout, user/register, user/change-password etc (btw same logic used in zfcuser module).
We are going to make an url like /user/profile/:user, where the :user part is the dynamic part of your url.
I assume you are working on the Application module and you have created and registered an UserController (if names are different the code below will need changes)
'router' => array(
'routes' => array(
// other routes configs here
'user' => array(
'type' => 'Literal',
'priority' => 1000,
'options' => array(
'route' => '/user',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'User',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'profile' => array(
'type' => 'Segment',
'options' => array(
'route' => '/profile/:user',
'defaults' => array(
'action' => 'profile',
),
),
),
),
),
Using this route config your application will respond with UserController::index when the url /user is called. Will respond with UserController::profile when /user/profile/someuser is called.
Note that the someuser part is mandatory because we said so in the rule. To make it not mandatory we should have written the rule like follow
'route' => '/profile/[:user]',
In the UserController's profile action you can take the :user value like follow
$this->params()->fromRoute('user')
This is just one of the solution you can adopt, for instance you could have written your rule without child routes, like follow
'user' => array(
'type' => 'Segment',
'options' => array(
'route' => '/user/profile/:user',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'User',
'action' => 'profile',
),
),
),
It is up to you to choose the solution that fits your needs. I suggest you also to read the ZF2 official routing documentation.