ZF2 routing in links is not working properly

I am working on a Zend Framework 2 application for work in which I cannot route correctly or do not know where to route it.

I have Hostname => webapp.foo-bar.com. We decided to add Subhost => / app / at the end, and the name of this application is called => app. I have a link on the page where it will point => / graph / page-name. But when I was linking to a link that looked like this:

<a href="/graph/page-name">FooBar</a>

I would get webapp.foo-bar.com/graph/page-name, not webapp.foo-bar.com/app/graph/page-name.

My application configuration:

<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'hostname',
                'options' => array(
                    'route'    => 'webapp.foo-bar.com/app',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller' => 'Index',
                        'action'     => 'index',
                    ),
                ),
            ),
        // The following is a route to simplify getting started creating
        // new controllers and actions without needing to create a new
        // module. Simply drop new controllers in, and you can access them
        // using the path /application/:controller/:action
        'application' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/application',
                '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(
                        ),
                    ),
                ),
            ),
        ),
    ),
),
'service_manager' => array(
    'abstract_factories' => array(
        'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
        'Zend\Log\LoggerAbstractServiceFactory',
    ),
    'aliases' => array(
        'translator' => 'MvcTranslator',
    ),
),
'translator' => array(
    'locale' => 'en_US',
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ),
    ),
),
'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => 'Application\Controller\IndexController'
    ),
),
'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',
    ),
),
// Placeholder for console routes
'console' => array(
    'router' => array(
        'routes' => array(
        ),
    ),
),
);

I saw that there is such an option as:

<a href="<?=$this->url('application/default', array('controller' => 'graph', 'action' => 'page-name')?>">FooBar</a>

Do we need to do this for each link, or can we do it at the configuration level?

Thank!


Update:

this- > url(), . jQuery. , , .

<a href="<?php echo $this->url('foo-bar', array('action' => 'bar-foo'))?>?year=2015"</a>

webapp.foo-bar.com/app/foo-bar?year=2015. , webapp.foo-bar.com/app/foo-bar/bar-foo?year=2015. foo-bar this-url()?

. !

+4
4

, . .

P.S. , .

use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use \Sample\Controller\IndexController;
use Zend\ServiceManager\Factory\InvokableFactory;


return [
    'router' => [
        'routes' => [
            'v2' => [
                'type' => Segment::class,
                'options' => [
                    'route' => '/v2/app',
                    'defaults' => [
                        'controller' => IndexController::class,
                        'action' => 'index',
                    ],
                ],
                'child_routes' => [
                    'index' => [
                        'type' => Literal::class,
                        'options' => [
                            'route' => '/',
                            'defaults' => [
                                'controller' => IndexController::class,
                                'action' => 'index',
                            ],
                        ],
                    ],
                    'application' => [
                        'type' => Segment::class,
                        'options' => [
                            'route' => '[/:action]',
                            'defaults' => [
                                'controller' => IndexController::class,
                                'action' => 'index',
                            ],
                        ],
                    ]
                ],
            ],
        ],
    ],
    'controllers' => [
        'factories' => [
            IndexController::class => InvokableFactory::class,
        ],
    ],
    'view_manager' => [
        'template_path_stack' => [
            __DIR__ . '/../view',
        ],
    ],
];

:

<?php echo $this->url("v2/application", ['action' => 'page-name']) ?>
+1

ZF2 , . graph/page-name , , GraphController pageNameAction(). . .

. ZF . . , Hostname , subdomain.domain.tld. domain.tld/v2/app. Hostname / 'route' => 'subdomain.domain.tld'. .

(domain.tld/v2/app) - , . MVC, Framework . (domain.tld/v1/abc domain.tld/v2/mno domain.tld/v3/xyz ..) ZF. . , , .

, domain.tld/graph/page-name, domain.tld/v2/app/graph/page-name. , v2/app URL-, . .

graph/page-name, v2/app/graph/page-name,

// Top key of a route
'v2app' => [
    'type' => 'Segment',
    'options' => [
        'route' => '/v2/app',                   
        'defaults' => [
            '__NAMESPACE__' => 'Application\Controller',
            'controller'    => 'Index',
            'action'        => 'index',
        ],
    ],
    'may_terminate' => true,
    'child_routes' => [
        'default' => [
            'type' => 'Segment',
            'options' => [
                'route' => '/',
                'defaults' => [
                    'controller' => 'Application\Controller\Index',
                    'action' => 'index',
                ],
            ],
        ],                              
        'graph' => [
            'type' => 'Segment',
            'options' => [
                'route' => '[/:controller/:action]',
                'defaults' => [
                    'controller' => 'Application\Controller\Graph',
                    'action' => 'pageName',
                ],
            ],
        ],
    ],
],

module.config.php, , .. , .

echo $this->url('v2app/graph', [], [], true);
echo $this->url(null, ['controller' => 'graph', 'action' => 'page-name']);

// outputs
// /v2/app/graph/page-name

v2app - , graph - .

, . , , . , . ( ) . graph/page-name

[
    'label' => 'Graph',
    // route name not route pattern
    'route' => 'graph', // not /graph/page-name
],

, , $this->url() helper, .

DOC Hostname.

, ,

+1

ViewModel, url

/**
 * @param  \Zend\Mvc\MvcEvent $e The MvcEvent instance
 * @return void
 */
public function setLayout($e)
{
    // Should return your customization
    $viewModel = $e->getViewModel();

$this->url() , :

$this->url('application/default', array('controller' => 'graph', 'action' => 'page-name')

:

$customStrategy = $app->getServiceManager()->get('{Your class name}'); 
$view->getEventManager()->attach($customStrategy, 1); // 1 - means priority

preg_replace_callback('/<a href=\\"([^\\"]*)\\">(.*)<\\/a>/iU', function ($matches) {
    // do whatever you need for each URL
}, $content);

DOM ( ):

$xpath = new \DOMXPath((new \DOMDocument)->loadHTML($content));
foreach ($xpath->query('//a[href]') as $link) {
    // ...
}
+1

. . , , Graph pageNameAction(), . .

graph/page-name v2/app/graph/page-name, , , .

<a href="/graph/page-name">FooBar</a>

, . src/YourModuleName/Route, YourModuleName\Route.

, 'template_prefix' => 'yourmodulename', . , patten yourmodulename/controller/action-name .

'v2app' => [
    'type' => 'Segment',
    'options' => [
        'route' => '[/v2/app]',             
        'defaults' => [
            'controller' => 'YourModuleName\Controller\Index',
            'action' => 'index',
        ],
    ],
    'may_terminate' => true,
    'child_routes' => [                         
        'graph' => [

            // We call the custom route plugin thus  
            'type' => 'YourModuleName\Route\AppRoute',
            'options' => [
                'dir_name'         => __DIR__ . '/../view',
                'template_prefix'  => 'yourmodulename',
                'filename_pattern' => '/[a-z0-9_\-]+/',                     
                'defaults' => [
                    'controller' => 'YourModuleName\Controller\Graph',
                    'action' => 'pageName',
                ],
            ],
        ],
    ],
],

pageNameAction()

public function pageNameAction()
{
    // Get path to view template from route params
    $pageTemplate = $this->params()->fromRoute('page', null);

    // If path is not valid
    if($pageTemplate == null) {
        $this->getResponse()->setStatusCode(404); 
        return;
    }       

    $viewModel = new ViewModel();

    // Here we set template to be rendered for `v2/app/graph/page-name`
    $viewModel->setTemplate($pageTemplate);

    return $viewModel;
}

If you need to display a link in a script view, use this method

echo $this->serverUrl('/graph/page-name');

Hope this helps you!

0
source

Source: https://habr.com/ru/post/1679456/


All Articles