OPtional trailing route

Hello , so can anyone help me ? I want to make a second param optional in my routes , is that possible ? Example :

/test/create/

optional trailing id: /test/modify/1234

My current route is :


$router->add("/test/{method:(create|modify|remove)}", [
  'controller' => 'index',
  'action'     => 'web',
]);


771

write your controller

edited Jun '16

Just:

$router->add("/test/{method:(create|modify|remove)}", [
  'controller' => 'index',
  'action'     => 'web',
]);
$router->add("/test/{method:(create|modify|remove)}/{id:int}", [
  'controller' => 'index',
  'action'     => 'web',
]);

And in controller parameter $id = 0

Also you shouldn't have 3 methods mapped to 1 action. Just 3 seperated methods to 3 actions.



771

Bad Solution!

Just:

$router->add("/test/{method:(create|modify|remove)}", [
 'controller' => 'index',
 'action'     => 'web',
]);
$router->add("/test/{method:(create|modify|remove)}/{id:int}", [
 'controller' => 'index',
 'action'     => 'web',
]);

And in controller parameter $id = 0

Also you shouldn't have 3 methods mapped to 1 action. Just 3 seperated methods to 3 actions.

edited Jun '16

Beacause ? It's faster to do it like this than doing regexp.



771
edited Jun '16

You can use similar API REST

Example: localhost/test/create?id=10

// services.php
$router->add("/test/{method:(create|modify|remove)}", [
  'controller' => 'index',
  'action'     => 'web',
]);

// TestController.php
class TestController extends ControllerBase {

        public function createAction(){
                    $id = $this->response->get("id");

                    if(empty($id)){
                            ...
                    }
                    ...             
        }
        ...
}
edited Jun '16

For me your solution is bad. You have not needed logic in action body.

Still he shouldn't just have one action for create, modify and remove. Each thing should have his own action.



771
edited Jun '16

Oh I'm sorry:

follow:

localhost/test/create \ localhost/test/modify \ localhost/test/remove


// services.php
$route->add("/test/(create|modify|remove)",
        array(
            'controller' => 'index',
            'action' => "web"
        ));

// IndexController.php
class IndexController extends ControllerBase {

        public function webAction(){
                    $id = $this->response->get("id");

                    if(empty($id)){
                            ...
                    }
                    ...             
        }
        ...
}


771

if you want to use your url like a localhost/test/create/10


//services.php
$route->add("/teste/(create|modify|remove)/{id:[0-9]*}",
        array(
            'controller' => 'index',
            'action' => "web",
            'param' => 2
        ));

//IndexController.php
class IndexController extends ControllerBase
{
    public function webAction($id){

        echo 'nome'.$id;
        die;
    }
}
edited Jun '16

I would just do:

$router->addPost("/test/create", [
  'controller' => 'index',
  'action'     => 'create',
]);
$router->addPut("/test/modify/{id:[0-9]*}", [
  'controller' => 'index',
  'action'     => 'modify',
]);
$router->addDelete("/test/delete/{id:[0-9]*}", [
  'controller' => 'index',
  'action'     => 'remove',
]);

class IndexController extends ControllerBase
{
    public function createAction(){
        // create logic
    }

    public function modifyAction($id){
        // modify logic
    }

    public function removeAction($id){
        // remove logic
    }
}

This should be done like this to be honest.



771
edited Jun '16

if you created controller with these function you don't need to create router config, ok?

The best practice is use Filter

https://docs.phalconphp.com/en/latest/reference/filter.html


// example

use Phalcon\Mvc\Controller;

class ProductsController extends Controller
{
    public function indexAction()
    {

    }

    public function showAction($productId)
    {
        $productId = $this->filter->sanitize($productId, "int");
    }
}

How i don't need to create router config ? Of course i need if i passed false to router constructor(i don't like default router behaviour). I just like defined routes more.



771

No Problem, but if you defined in controller, you don't need to config again, but your choice

How i don't need to create router config ? Of course i need if i passed false to router constructor(i don't like default router behaviour). I just like defined routes more.

But how controller knows what type of action this is gonna be without config ? :)

No Problem, but if you defined in controller, you don't need to config again, but your choice

How i don't need to create router config ? Of course i need if i passed false to router constructor(i don't like default router behaviour). I just like defined routes more.



771
edited Jun '16

It's the magic, It knows =).. if you created controller with IndexController name and a function createAction, your url is localhost/index/create -- ok?

You can see it this link: https://docs.phalconphp.com/pt/latest/reference/controllers.html

But how controller knows what type of action this is gonna be without config ? :)

No Problem, but if you defined in controller, you don't need to config again, but your choice

How i don't need to create router config ? Of course i need if i passed false to router constructor(i don't like default router behaviour). I just like defined routes more.

edited Jun '16

You wrong, i checked source, there is no magic, https://github.com/phalcon/cphalcon/blob/2.1.x/phalcon/mvc/router.zep you can as well do create as get or put and delete as get or put or post etc. It doesn't check things like that. Phalcon don't do anything magic like other frameworks. It just accepts any method type if we are using router with default behaviour.

It's the magic, It knows =).. if you created controller with IndexController name and a function createAction, your url is localhost/index/create -- ok?

You can see it this link: https://docs.phalconphp.com/pt/latest/reference/controllers.html

But how controller knows what type of action this is gonna be without config ? :)

No Problem, but if you defined in controller, you don't need to config again, but your choice

How i don't need to create router config ? Of course i need if i passed false to router constructor(i don't like default router behaviour). I just like defined routes more.

Im using phalcon more than year, checked behaviour of most classes in it, there is no magic in phalcon like in other frameworks :)



771

I don't go to discuss it with you, but if you want to use differents methos like (put, post, delete), You must define router =).. I Knew it.. but i thing you want to use only GET method.

Good Programing for you =)..

Why you even want to create or delete something as get method ?!

edited Jun '16

It's the magic, It knows =)..

NHF, but after this argument, your posts are worth as dirt.

@HudsonNicoletti

The only reasonable solution for your question is what @Jurigag provided in first reply.



771

That was a joke, but.. everyone knows it's programming.. He was talking that he needs to define route then i talked he doesn't need if was using get method (HTTP).

It's the magic, It knows =)..

NHF, but after this argument, your posts are worth as dirt.

@HudsonNicoletti

The only reasonable solution for your question is what @Jurigag provided in first reply.

Still, i would prefer 3 actions for 3 methods, then doing remove/create/modify in one action.

It's the magic, It knows =)..

NHF, but after this argument, your posts are worth as dirt.

@HudsonNicoletti

The only reasonable solution for your question is what @Jurigag provided in first reply.



771

correcting code:

url: /test/create/ url: /test/create/1 ...


// services.php

$router->add("/test/{method:(create|modify|remove)}/{id:[0-9]*}", [
  'controller' => 'index',
  'action'     => 'web',
  'param' => 2 // <-- ok!
]);

OK ?

Just:

$router->add("/test/{method:(create|modify|remove)}", [
 'controller' => 'index',
 'action'     => 'web',
]);
$router->add("/test/{method:(create|modify|remove)}/{id:int}", [
 'controller' => 'index',
 'action'     => 'web',
]);

And in controller parameter $id = 0

Also you shouldn't have 3 methods mapped to 1 action. Just 3 seperated methods to 3 actions.