I would like to add a new route (link) to my ZF2 application like following:
mysite.com/somename/?invitecode=12345
Please note that /somename/ shouldn't be controller, but merely just a name in link which is used for tracking purposes. I figured I could do this by adding a new controller, but since this name is going to be dynamic, I can't use controller for this. I have found this in the module.config.php file:
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
I tried adding a path here like this just for testing purposes:
'application/index/indextest' => __DIR__ . '/../view/application/index/index.phtml',
And I've tried accessing the URL like this:
mysite.com/index/indextest
But the only way I can access this link is if I add an action in the controller like this:
public function indextestAction()
{
return $this->redirect()->toUrl("/");
}
Please note that the:
mysite.com/THISNAMEHERE/?invitecode=12345
Please note that THISNAMEHERE is dynamic, and varies upon what is written in my vhost's config file.
What am I supposed to do here? Can someone help me out with this please?
EDIT:
Guys I've done the following so far, I have added a new controller with a name of "InviteController" which does the following check:
public function indexAction()
{
if(!empty(htmlspecialchars($_GET["inviter"])))
{
return $this->redirect()->toUrl("/user/emailsignup");
}
}
I've added the controller to the invokables list like following:
'Application\Controller\Invite' => 'Application\Controller\InviteController',
And in my module.config.php file:
'invite' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/invite/',
'defaults' => array(
'controller' => 'Application\Controller\Invite',
'action' => 'index',
),
),
),
So now when I try to access the URL it is like following:
mysite.com/**invite**/?inviter=12345
However this is still not what I want... I need this bolded part (INVITE) to be dynamic. Basically if I'm accessing the app from a different vhost it would be like this:
mysite.com/vhost1name/?inviter=1234
And I'd still like it to invoke the InviteController and Index action within that controller.
Edit #2: Finally solved it! Thanks to @Wilt for the links and explanation! :)
Just in case anyone wonders, this is the solution:
'application' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Application\Controller\Invite',
'action' => 'index',
),
),
),
),