Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Add constant in route

61 views
Skip to first unread message

BQ VOR

unread,
Oct 1, 2024, 10:49:40 AM10/1/24
to Fat-Free Framework
Hi Guys,

probably too simple but i just can't find it. I have my routes in an .ini file and would like to have some exception routes where i can inject a constant to appear in the $param array.

So for example;

GET /@item = Controllers\Index->overview
GET /page = Controller\Index->overview (and inject type=page)

This way i have one controller with method 'overview' and the second route will have $params['type'] filled with 'page' whilst the first route does not result in $params['type'].

I see named routes but i don't think that will get me there.

Please bare in mind i use .ini so the syntax has to work in there...

Thanks in advance

v.

unread,
Oct 2, 2024, 2:52:29 AM10/2/24
to Fat-Free Framework
Just add the /page before the /@item in the ini file.
Or check in your controller if @item == "page" and handle it there.

BQ VOR

unread,
Oct 2, 2024, 8:00:42 AM10/2/24
to Fat-Free Framework
Hi V.

this is not what i am looking for i think. What i want is to pass a specific value onto f3 when a route is true.

GET /articles Controllers\app->index [type=articles]
GET /pages Controllers\app->index [type=pages]

Unfortunately i cannot connect it to a wildcard such as

GET /@item Controllers\app->index

where in $params['item'] i can find 'articles' or 'pages' or else.

So the ideal scenario would be that i can pass a key -> value pair from within the route to the $params array.

Op woensdag 2 oktober 2024 om 08:52:29 UTC+2 schreef v.:

v.

unread,
Oct 2, 2024, 8:16:09 AM10/2/24
to Fat-Free Framework
I'm sure I'm misunderstanding, but wouldn't this (totally untested) code do what you are looking for?
public function index()
{
    if ($f3->get("PARAMS.item"=="page"){
       $array['type']='pages';
    }
    else {
       $array['type']='articles';
    }
//then do your stuff
}

Or send the 2 options to different functions:
GET /page = Controller\Index->pages
GET /@item = Controllers\Index->atricles

BQ VOR

unread,
Oct 4, 2024, 10:48:12 AM10/4/24
to Fat-Free Framework
Hi V.

looking at your code i suspect this will not work. The controller is Index (not a public function/method but a class) and the first route will not tell the controller that is was 'page' on the route. The second one will fill @item with the desired value from the route. If i can use the first route and somehow fill that @item=page when that route is true than my problem is solved.

Op woensdag 2 oktober 2024 om 14:16:09 UTC+2 schreef v.:

ved

unread,
Oct 4, 2024, 1:52:06 PM10/4/24
to Fat-Free Framework
Hi,

It would be useful if you could try to explain in a bit more detail what exactly you wish to accomplish as I'm guessing this is not what you want since is similar to other answers, but how about something like this:

routes.ini:

GET /@item Controllers\app->index


Controllers\app.php:
class App {

    function index() {
        $f3 = \Base::instance();
        $slug = $f3->get('PARAMS.item');

        // very simplified, run checks on $slug before passing it on
        $this->doWork($slug);
    }

    function doWork($type) {
        echo $type;
    }
}



With the above:

Accessing /articles will call the doWork() method with $type=articles
Accessing /pages will call the doWork() method with $type=pages
Accessing /anythingelse will call the doWork() method with $type=anythingelse 
(?)

Cheers.


v.

unread,
Oct 5, 2024, 8:24:30 AM10/5/24
to Fat-Free Framework
well yes, I understood that much, you need to put the function inside the class.....
I think this should roughly do what you need.

class Index
{
  public function overview()
  {
    if ($f3->get("PARAMS.item"=="page"){ $array['type']='pages';  } //personally i would use a private function, like $this->doPageStuff()
      else { $array['type']='articles'; } // or $this->doArticleStuf()
   }
//.... do your thing

BQ VOR

unread,
Oct 10, 2024, 4:32:16 AM10/10/24
to Fat-Free Framework
Hi,

thanks for both answers.

@Ved: what i want is a route containing a wildcard, let's say @item, will pass the value of this wildcard in PARAMS. This route will handle let's 20 different procedures, all based on what's coming in on PARAMS.item. However i have a two exceptions where the route pattern is 100% alike but i want to capture these exceptions. To do this i add two extra routes where @items is not in the route as wildcard but a static value. The route itself works but the app does not get PARAMS.item since the wildcard does not exist. I would like to manually add this from the route instruction so the two exceptions also have PARAMS.item, but then filled with the static value i assign. I don't know how better to explain it. 

The routing table would be something like this (simplified);

Wildcard route: app/@item > Controller->show
/app/show/orders
/app/show/bikes
/app/show/cars
/app/show/cabinets

These URLS are all managed by the route. PARAMS.item will contain orders/bikes/cars/cabinets

I have two exceptions;

/app/show/advancedproducts: has to go to Controller->showAdvanced
/app/show/advancedmaterials: has to go to Controller-> showAdvanced

At this point i don't have PARAMS.item available so showAdvanced() does not 'know' if it needs to show advancedproducts or advancedmaterials.

To explain even further, the routing is auto-generated so the four first routes can be 10, 20 or a 100. Depending on which data the app will revolve the app will serve such routes for about all models.

Op zaterdag 5 oktober 2024 om 14:24:30 UTC+2 schreef v.:

v.

unread,
Oct 10, 2024, 6:07:30 AM10/10/24
to Fat-Free Framework
I fail to see why you are not just using one of the 2 solutions I have already given you.
Just put the static route first in the ini file. It will only pass on to the dynamic routes if the static route doesn't exist.

A third option would be to check if params.item exists in the controller and do what you need.

Paul Herring

unread,
Oct 10, 2024, 6:13:41 AM10/10/24
to BQ VOR via Fat-Free Framework
> At this point i don't have PARAMS.item available so showAdvanced() does not 'know' if it needs to show advancedproducts or advancedmaterials.

You can obtain the URI from Base::Instance (the $f3 parameter that gets passed along) and parse that? For example from

\Base::instance()->get('URI'); or
$f3->get("URI");

Or any of the other variables that has the URI in it. var_dump($f3) within that function and find out which variables have advancedproducts in it in a format you can use most easily? (A quick investigation suggests PARAMS.0, PATH, REALM, URI, SERVER.REQUEST_URI all contain the same thing.)



--
-- You've received this message because you are subscribed to the Google Groups group. To post to this group, send an email to f3-fra...@googlegroups.com. To unsubscribe from this group, send an email to f3-framework...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/f3-framework?hl=en
---
You received this message because you are subscribed to the Google Groups "Fat-Free Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to f3-framework...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/f3-framework/fd170008-6f66-485a-ac6b-57bf1031c886n%40googlegroups.com.


--
PJH

ved

unread,
Oct 10, 2024, 10:54:35 AM10/10/24
to Fat-Free Framework
Hi,

I think you're overcomplicating things.

Set a route like so:

/app/show/@type = \Controller->show

Then on the show() method:

if(in_array($f3->get('PARAMS.type', $type), ['advancedproducts', 'advancedmaterials'])) {
    $this->showAdvanced($type);   // this will call the showAdvanced method with the appropriate $type 
} else {
   // do whatever when type is not advanced***
}

The above should accomplish exactly what you are asking IMHO.

Cheers.
Reply all
Reply to author
Forward
0 new messages