php - Adding a new route in Zend Framework2

856

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',
                        ),
                    ),
                ),
            ),
668

Answer

Solution:

The routing for your ZF2 application is configured in your router config. The router config for a ZF2 module is most commonly stored inside themodule.config.php file inside the moduleconfig 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 aview_manager config not arouter 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.

People are also looking for solutions to the problem: php - Get first li Simple DOM Parser

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.