1

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',
                        ),
                    ),
                ),
            ),
perkes456
  • 1,163
  • 4
  • 25
  • 49

1 Answers1

1

The routing for your ZF2 application is configured in your router config. The router config for a ZF2 module is most commonly stored inside the module.config.php file inside the module config folder:

/module/MyModuleName/config/module.config.php

The router config looks like this:

// This is your router config
'router' => array(
    'routes' => array(
        // config goes here
    ),
),

You can read more on routing in the ZF2 documentation chapter Routing and controllers

What you refer to in your question is a view_manager config not a router config.

If you are not familiar with basic concepts like routing and router config I would strongly suggest to follow a tutorial to get to know these basics before you work on your own application. The ZF2 tutorial skeleton application (also known as the album application) is a good starting point. Following the steps in the tutorial will help you achieve what you want.

Wilt
  • 41,477
  • 12
  • 152
  • 203
  • Hey @Wilt I have setup a route but its still not what I require. I've updated m question above. can you check it out ? – perkes456 May 23 '16 at 13:02
  • @perkes456 I don't see a route config in your question. Can you add it? – Wilt May 23 '16 at 13:04
  • Hi Wilt, my apologies, I've updated the question , its there now. – perkes456 May 23 '16 at 13:08
  • P.S. I've updated the answer with what I'd like to make out of route that I'm entering, I hope it will make sense... – perkes456 May 23 '16 at 13:08
  • So basically what I'm trying to make is to make that part of url dynamic, but to make it still invoke the index action within the invite controller.... like instead of /invite/?inviter=1238 to invoke dynamically like this /whichevernameishere/?inviter=1238 -> invoke index action of the Invite controller... – perkes456 May 23 '16 at 13:15
  • @perkes456 You defined a static route, you should make your route dynamic. There are a lot of examples online, just search for ***dynamic routing ZF2***. – Wilt May 23 '16 at 13:23
  • Yes its static unfortunately... I have searched a lot but haven't found a solution to what I'm looking for... Could you help me out ? – perkes456 May 23 '16 at 13:24
  • @perkes456 If you want dynamic host names you should do something like in [this blog post](http://briangallagher.ie/2013/01/29/hostname-subdomain-routing-and-urls-using-zf2/) or [this stackoverflow answer](http://stackoverflow.com/a/13071724/1697459) – Wilt May 23 '16 at 13:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112693/discussion-between-perkes456-and-wilt). – perkes456 May 23 '16 at 13:30
  • thanks for the links, I've finally solved it! You're the best! :) – perkes456 May 23 '16 at 14:08