Fatfree route optional token

54 views
Skip to first unread message

FreeFAT

unread,
Sep 20, 2019, 6:24:19 AM9/20/19
to Fat-Free Framework
Hi everyone!

My URLs coud be like 
1) /site/guestbooks/add
2) /site/guestbooks/edit/23
3) /site/guestbooks/delete/23
4) /site/guestbooks/show/100

I wish construct one route and at the end of url (1) there is no token.
I want to have a one pattern of tokens.

If I constrict like that:
GET /site/@table/@action/@param=Admin->test
then this url /site/guestbooks/add doesn't work and shows 404 error.

What to do?

I need one route pattern for all routes like /site/guestbooks/add and /site/guestbooks/edit/23

Thanks.

FreeFAT

unread,
Sep 20, 2019, 6:30:43 AM9/20/19
to Fat-Free Framework
is there for example smth like

GET /site/@table/@action/{@param}=Admin->test

{...} -- means that it is optional... it could be not passed

Is there something in fatfree framework?

Thanks

ved

unread,
Sep 20, 2019, 7:24:38 AM9/20/19
to f3-fra...@googlegroups.com
Hi,

I'm not really sure if you can achieve that with only one route pattern.
But if you're ok with two, then you can try something like this:

GET /site/@table/@action = @table->@action
GET
/site/@table/@action/@param = @table->@action

Then, per your examples:

1) /site/guestbooks/add ----  will call Guestbooks->add()
2) /site/guestbooks/edit/23 ---- will call Guestbooks->edit() --- inside this edit() method you can access $f3->get('PARAMS.param') to get the value of @param
3) /site/guestbooks/delete/23 ---- will call Guestbooks->delete() --- inside this delete() method you can access $f3->get('PARAMS.param') to get the value of @param
4) /site/guestbooks/show/100 ---- will call Guestbooks->show() --- inside this show() method you can access $f3->get('PARAMS.param') to get the value of @param

Cheers

FreeFAT

unread,
Sep 20, 2019, 7:30:53 AM9/20/19
to Fat-Free Framework
GET /site/@table/@action = @table->@action
GET 
/site/@table/@action/@param = @table->@action

I want to make it in one pattern line. Is it possible?

You wrote 2 lines and what about make it in one like 

GET /site/@table/@action(:/@param) = @table->@action

I wish /site/guestbooks/add ---- opens Guestbooks->add() only by one pattern

Is there some solving?

ved

unread,
Sep 20, 2019, 8:04:15 AM9/20/19
to Fat-Free Framework
As I've stated on my previous reply, I'm not really sure if that can be done with just one route as I didn't find any docs on optional parameters.

Maybe route grouping is what you need:


Cheers

FreeFAT

unread,
Sep 20, 2019, 9:42:59 AM9/20/19
to Fat-Free Framework
There will be some conflict:

GET /site/@table/@action = @table->@action
GET 
/site/@table/@action/@param = @table->@action


/site/guestbooks/add ----  will call Guestbooks->add()
/site/guestbooks/add/343 ----  will call Guestbooks->add()

That's why I should do like

GET /site/@table/@action = @table->add
GET 
/site/@table/@action/@param = @table->@action (then here I should check @action for 'add' value)


пятница, 20 сентября 2019 г., 15:24:38 UTC+4 пользователь ved написал:

ved

unread,
Sep 20, 2019, 10:23:15 AM9/20/19
to f3-fra...@googlegroups.com
Hi,

No conflicts, if you access it through: '/site/guestbooks/add' or  '/site/guestbooks/add/343', they will both call Guestbooks->add() which is perfectly acceptable.

Inside the add() method, you can then check for the presence of PARAMS.param. If it's set, it will be '343' in this example.

Example:

public function add() {
    $f3
= \Base::instance();
   
if($f3->exists('PARAMS.param', $param)) {
        $this
->somethingWithParam($param);
   
} else {
        $this
->somethingWithoutParam();
   
}
}

private function somethingWithParam($param) {
   
// do stuff with $param
}

private function somethingWithoutParam() {
   
// do stuff without $param
}

ikkez

unread,
Sep 21, 2019, 4:28:48 AM9/21/19
to Fat-Free Framework
if you want, you can try it like this:

GET /site/@table/@action* = @table->@action

and then check $f3->get('PARAMS') for additional parameters of any length.
Reply all
Reply to author
Forward
0 new messages