在控制台操作 zf3 中找不到路由
can't find route in console action zf3
我正在尝试从 ZF3 中的控制台生成站点地图。
控制台操作被执行,但当我尝试生成 url 时它中断了
用 $this->url()->fromRoute()...
这是控制器动作
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');
}
这是路线
'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',
],
],
],
我得到异常:
php /var/www/pcwebshop/public/index.php 站点地图
应用程序抛出异常!
Zend\Router\Exception\RuntimeException
找不到名称为 "kategorija" 的路线
知道哪里出了问题吗?
问题是在执行控制台操作时没有可供路由器使用的 http 路由。这不是最佳实践解决方案,因为 getServiceLocator 已被弃用,但它(目前)有效。
public function sitemapAction() {
$event = $this->getEvent();
$http = $this->plugins->getServiceLocator()->get('HttpRouter');
$router = $event->getRouter();
$event->setRouter($http);
$loc = $this->model->dobijGeneralnuPostavku('sitemap_web');
.....
我正在尝试从 ZF3 中的控制台生成站点地图。 控制台操作被执行,但当我尝试生成 url 时它中断了 用 $this->url()->fromRoute()...
这是控制器动作
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');
}
这是路线
'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',
],
],
],
我得到异常: php /var/www/pcwebshop/public/index.php 站点地图
应用程序抛出异常! Zend\Router\Exception\RuntimeException 找不到名称为 "kategorija" 的路线
知道哪里出了问题吗?
问题是在执行控制台操作时没有可供路由器使用的 http 路由。这不是最佳实践解决方案,因为 getServiceLocator 已被弃用,但它(目前)有效。
public function sitemapAction() {
$event = $this->getEvent();
$http = $this->plugins->getServiceLocator()->get('HttpRouter');
$router = $event->getRouter();
$event->setRouter($http);
$loc = $this->model->dobijGeneralnuPostavku('sitemap_web');
.....