zend-framework3php-7.1zend-controller

can't find route in console action zf3


i'm trying to make sitemap generator from console in ZF3. Console action gets executed but it breaks when i try to generate url with $this->url()->fromRoute()...

here is controller action

  public function sitemapAction() {

    $loc = $this->model->dobijGeneralnuPostavku('sitemap_web');
    $xml_data = new \SimpleXMLElement('<?xml version="1.0"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>');

    $staticke = $this->model->sitemapStaticke();
    foreach ($staticke as $stat) {
        $a = ['url' => [
                'loc' => $loc . $stat,
            ]
        ];
        $this->array_to_xml($a, $xml_data);
    }

    $kategorije = $this->model->sitemapKategorije();
    foreach ($kategorije as $pod) {
        $a = ['url' => [
                'loc' => $loc . $this->url()->fromRoute('kategorija', ['idkat' => $pod['id'], 'ime' => $pod['ime'], 'page' => 1]),
            // 'lastmod'=> date('Y-m-d', strtotime(date("Y-m-d").'- 2 days' )) ,
            ]
         ];
        $this->array_to_xml($a, $xml_data);
    }

    $artikli = $this->model->sitemapArtikli();
    foreach ($artikli as $artikl) {
        $a = ['url' => [
                'loc' => $loc . $this->url()->fromRoute('artikl', ['id' => $artikl['id'], 'ime' => preg_replace(['/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'], ['', '-', ''], $artikl['ime'])]),
            ]
        ];
        $this->array_to_xml($a, $xml_data);
    }
    //unlink('/var/www/name.xml');
    $result = $xml_data->asXML(__DIR__ . '../../../public/sitemap.xml');
}

here are routes

'kategorija' => [
            'type' => Segment::class,
            'options' => [
                'route' => '/kategorija/:idkat/:ime[/stranica/:page]',
                'defaults' => [
                    'controller' => Controller\IndexController::class,
                    'action' => 'kategorija',
                ],
            ],
        ],
        'artikl' => [
            'type' => Segment::class,
            'options' => [
                'route' => '/artikl/:id/:ime',
                'defaults' => [
                    'controller' => Controller\IndexController::class,
                    'action' => 'artikl',
                ],
            ],
        ],

and i get exception: php /var/www/pcwebshop/public/index.php sitemap

The application has thrown an exception! Zend\Router\Exception\RuntimeException Route with name "kategorija" not found

Got any insight what might be wrong?


Solution

  • Problem was there wasn't http routes available to router when executing console action. This isn't best practice solution as getServiceLocator is deprecated but it works(for now).

      public function sitemapAction() {
    
        $event = $this->getEvent();
        $http = $this->plugins->getServiceLocator()->get('HttpRouter');
        $router = $event->getRouter();
        $event->setRouter($http);
    
        $loc = $this->model->dobijGeneralnuPostavku('sitemap_web');
    .....