We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

remove index in url

hi, i have a controller and is name 'item' ( url: item/index/1593 )

i want to show url like this: item/1593

actiually 1593 is Parametr.

my route is :

$publicResources = array( 'item' => array('index') );

foreach ($publicResources as $resource => $actions) { $acl->addResource(new Resource($resource), $actions); }

how can i do this ?

sry for bad type english .



1.4k
Accepted
answer

Hi,

Your code concerns the ACL, and not the routing :

To define an URL like item/1345, you can add a route like this :

$router = new Router();
...
//Define a route
$router->add(
    "/item/:int",
    array(
        "controller" => "item",
        "action"     => "index",
        "id"     => 1
    )
);
...
$router->handle();

where id is the named parameter of type int

you can also use the short syntax :

$router->add("/item/{id:[0-9]+}", "Item::index");

you can then access the id parameter as follows:

class ItemController extends Controller{

    public function indexAction(){

        // Returns "id" parameter
        $id = $this->dispatcher->getParam("id");
        ...
    }
}

For more details, see https://docs.phalcon.io/en/latest/reference/routing.html



7.0k
edited Apr '15

Tanx jcheron :)