Answer the question
In order to leave comments, you need to log in
phalcon routing?
I'm learning Phalcon, and immediately a question arose, a problem with routing.
I want the url /product/{id}-{title}
to call the showAction method of the ProductController with the id and title passed there, which will unload the product with id={id} from the database,
for example site.ru/product/1022-notebook-samsung-gs5411-white
id = 1022
title = notebook-samsung-gs5411-white
but the problem is the following. routing works like /controller/action and it doesn't care about my {id}
tried to turn off routing altogether, it still works according to the standard method. Maybe I am declaring it incorrectly?
tell me what am I doing wrong?
ProductController:
<?php
class ProductController extends \Phalcon\Mvc\Controller
{
public function indexAction()
{
echo "Главная страница продуктов";
}
/**
* Страница одного продукта
*/
public function showAction()
{
$pid = $this->dispatcher->getParam("pid");
$name = $this->dispatcher->getParam("title");
echo "Страница продукта $pid ($title)";
}
}
...
$router = new \Phalcon\Mvc\Router();
$router->add(
"/product/",
array(
"controller" => 'product',
"action" => 'index',
)
);
$router->add(
"/product/{id}-{title}",
array(
"controller" => "product",
"action" => "show",
)
);
$router->handle();
...
/product/13
PhalconException: Action '13' was not found on handler 'product'
Answer the question
In order to leave comments, you need to log in
Carefully read the documentation on routing
docs.phalconphp.ru/ru/latest/reference/routing.html
After reading it, you will immediately understand that you are forming the routing rule incorrectly, therefore it is processed incorrectly
No, I had everything right with the rules. just the routing had to be wrapped in a DI wrapper:
$di->set('router', function () {
$router = new \Phalcon\Mvc\Router(false);
// маршруты...
return $router;
});
$router->add(
'/product/:int\-([\w\d_-]+)',
[
'controller' => 'product',
'action' => 'show',
'id' => 1,
'title' => 2,
]
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question