M
M
My joy2015-03-29 16:24:40
PHP
My joy, 2015-03-29 16:24:40

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)"; 
      
    }
    
  }

And the routing itself:
...
  $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();
...

a request to the url
/product/13
gives:
PhalconException: Action '13' was not found on handler 'product'

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Merkulov, 2015-03-29
@StranikS

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

M
My joy, 2015-03-29
@t-alexashka

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;
});

S
Sergey Semenko, 2015-03-29
@abler98

$router->add(
  '/product/:int\-([\w\d_-]+)',
  [
    'controller' => 'product',
    'action' => 'show',
    'id' => 1,
    'title' => 2,
  ]
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question