Same named routed for more routing url

52 views
Skip to first unread message

SoftWord

unread,
Apr 4, 2019, 6:27:27 AM4/4/19
to f3-fra...@googlegroups.com

Hi, as in subject, can I have same named routing for more url? Like that:


$this->AddRoute('GET @admin: /admin', __NAMESPACE__ . '\Pages->Admin');
$this->AddRoute('GET @admin: /admin/eslstatus', __NAMESPACE__ . '\Pages->ESLStatus');

My target is to use alias to define the ACL of every page by using $f3->get('ALIAS'), it seams work, but I'm not sure of that.

Paul Herring

unread,
Apr 4, 2019, 7:20:13 AM4/4/19
to SoftWord via Fat-Free Framework, Fat-Free Framework
Not normally a problem...

$ cat index.php
<?php
$f3 = require('f3/lib/base.php');

function view_function($f3){
        echo $f3->SERVER['REQUEST_URI']."\n";
}
$f3->route('GET /', function($f3) { echo "/\n";});
$f3->route('GET /top', function($f3) { echo "top\n";});
$f3->route('GET /top/middle',function($f3) { echo "middle\n";});
$f3->route('GET /top/middle/bottom',function($f3) { echo "bottom\n";});
$f3->run();



What you may find a bit easier, however:

$ cat index.php
<?php
$f3 = require('f3/lib/base.php');

class page {
        function default($f3){
                echo "default() called\n";
        }
        function a($f3){
                echo "a() called\n";
        }
        function b($f3){
                echo "b() called\n";
        }
}

$f3->route('GET /', function($f3) { echo "/\n";});
$f3->route('GET /admin', 'page->default');
$f3->route('GET /admin/@page', 'page->@page');
$f3->run();


$ curl hpdesktop.dontexist.com/f3/admin
default() called
$ curl hpdesktop.dontexist.com/f3/admin/a
a() called
$ curl hpdesktop.dontexist.com/f3/admin/b
b() called
$ curl hpdesktop.dontexist.com/f3/admin/c
<!DOCTYPE html>
<html>
<head><title>405 Method Not Allowed</title></head>
<body>
<h1>Method Not Allowed</h1>
<p>HTTP 405 (GET /admin/c)</p>
</body>
</html>




On Thu, Apr 4, 2019 at 11:27 AM SoftWord via Fat-Free Framework <f3-framework+APn2wQcnFeBkUoDM53w...@googlegroups.com> wrote:

Hi, as in subject, can I have same named routing for more url? Like that:


$this->AddRoute('GET @admin: /admin', __NAMESPACE__ . '\Pages->Admin');
$this->AddRoute('GET @admin: /admin/eslstatus', __NAMESPACE__ . '\Pages->ESLStatus');

My target is to use alias to define the ACL of every page.

--
-- 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 post to this group, send email to f3-fra...@googlegroups.com.
Visit this group at https://groups.google.com/group/f3-framework.
To view this discussion on the web visit https://groups.google.com/d/msgid/f3-framework/4a90ca87-72cc-4163-9eee-445db7a55e58%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
PJH

SoftWord

unread,
Apr 4, 2019, 8:05:09 AM4/4/19
to Fat-Free Framework
My logic is a bit complex.

Page class extend another class, Controller:

class Page extends Controller {...}


In constructor controller I make some engine logics, a new logic that I need to introduce is:

for all pages processed by class Page, Controller::constructor(...) need to make some check, if pass, go to render page, else turn in homepage '/', so with one function I can control all pages.

My implementation idea is like that:

$currentAlias = $f3->get('ALIAS');

if ( $currentAlias == 'admin' ) render page...;
else reroute('/');

and my first question is if I can use @namedroutes to specify my roles of ACL.


Paul Herring

unread,
Apr 4, 2019, 8:08:35 AM4/4/19
to SoftWord via Fat-Free Framework, Fat-Free Framework
On Thu, Apr 4, 2019 at 1:05 PM SoftWord via Fat-Free Framework <f3-framework+APn2wQcnFeBkUoDM53w...@googlegroups.com> wrote:
My logic is a bit complex.

Ugh - sorry; I completely missed the whole point of your question - ignore my answer.


SoftWord

unread,
Apr 4, 2019, 8:31:26 AM4/4/19
to f3-fra...@googlegroups.com
No problem mate :)

Have some experience about Named routing of F3?

Or ACL of every single page -> user roles?

ved

unread,
Apr 4, 2019, 8:49:52 AM4/4/19
to Fat-Free Framework
Hello SoftWord my old friend. I've come to reply to you again

I'm not @ikkez or an F3 developer, so this reply is probably not what you want. >:-)

That said, if you have the same alias, what you won't be able to do is to route to some place using the alias.
For example: $f3->reroute('@something') because F3 probably won't be able to know which route you mean.
If you're not using the aliases for routing then what is returned by the ALIAS variable should be what you wish (as you appear to already be doing).

Still, I'd recommend taking a look at @xfra35's excellent f3-access plugin that may be able to achieve what you're trying to do in a less "hacky" way.

Good luck.

SoftWord

unread,
Apr 4, 2019, 9:05:23 AM4/4/19
to Fat-Free Framework
Hahhaha you are wellcome ved I remember you :)

Thanks for help me :)

Nice plugin that.

Richard Goldstein

unread,
Apr 4, 2019, 9:24:55 AM4/4/19
to f3-fra...@googlegroups.com, f3-framework+APn2wQcnFeBkUoDM53w...@googlegroups.com

Why don’t you group the various method in a controller class (or classes), and in that class (or in the base class) do your check in beforeRoute(), which can reroute to ‘/’ if needed?

richgoldmd

unread,
Apr 4, 2019, 9:32:07 AM4/4/19
to Fat-Free Framework
For example - if you have routes 
/admin/something
and 
/admin/somethingElse


pointing to a controller:

class MyController {
   
public function beforeRoute($f3, $args) {
     
// Do some check in here - you could split $f3->get('PATH') or $args[0] if needed and use the first portion
     
if ($myACLCheckFailed) {
         $f3
->reroute('/');
     
}
   
}
   
   
public function adminHandler($f3, $args) {
     
// ACL has already been checked....
   
}
};

SoftWord

unread,
Apr 4, 2019, 9:38:49 AM4/4/19
to Fat-Free Framework
I do exactly that but in  constructor of my Controller, the results it's same.

richgoldmd

unread,
Apr 4, 2019, 9:41:21 AM4/4/19
to Fat-Free Framework
Does that solve the issue, or am I not understanding the problem?

I suppose the ctor would work, but its outside of the route flow (other than the hidden instantiation of the class). beforeRoute() gives you all of the parameters directly. 

SoftWord

unread,
Apr 4, 2019, 10:14:22 AM4/4/19
to Fat-Free Framework
Yes I've solved, our Engine is comples, splitting hive variable PATH is not so brilliant, because we have uri like /admin/section/01/etc... we have need more granularity, using alias we can make so much things.......
My primer doubt it's only I can use same @aliasnamed with more than one route, the response is yes.

Using the access plugin, ok, it's best way for sure, but now we have time to integrate it, we can do for sure next version of our Engine :)

Thanks all for advices :)
Reply all
Reply to author
Forward
0 new messages