bernhard Posted April 5, 2021 Share Posted April 5, 2021 I want to hook urls having this schema: /mymodule/{foo}/{bar}/{optional:urlSegment} This is what I came up with: $url = "/mymodule/(.*)/"; $this->wire->addHook($url, function(HookEvent $event) { $url = $event->arguments(1); $parts = explode("/", $url); if(count($parts) < 3 OR count($parts) > 4) { throw new Wire404Exception("Invalid number of url segments"); } ... It makes sure that I have a trailing slash and it fires on /mymodule/foo/bar/ as well as /mymodule/foo/bar/test/ Any better ideas/solutions for such "optional" url segments? PS: I have to say it again: What a brilliant feature these new url hooks!!! ? Link to comment Share on other sites More sharing options...
WillyC Posted April 5, 2021 Share Posted April 5, 2021 Bern hard u.can does $wire->addHook('/mymodule/{foo}/{bar}(/{baz})?/', function($e) { return "<pre> foo: $e->foo bar: $e->bar baz: $e->baz "; }); Quote no.quoter Quote del-msg Quote rm -Rf cd .. @ bern berna hard @ @bernhard 6 2 Link to comment Share on other sites More sharing options...
cb2004 Posted April 8, 2021 Share Posted April 8, 2021 On 3/30/2021 at 8:01 PM, adrian said: Has anyone checked 3.0.174 to see if this is now fixed? I upgraded to 3.0.175 on a site where pagination broke (I did a roll back) and I can confirm it is working again. 2 Link to comment Share on other sites More sharing options...
bernhard Posted April 9, 2021 Share Posted April 9, 2021 Thx @WillyC this works great and one can even define multiple (non-)optional url segments ? /endpoint/{one}(/{two})?(/{three})?/ Link to comment Share on other sites More sharing options...
Richard Jedlička Posted December 29, 2021 Share Posted December 29, 2021 Hi @ryan and others, this is great addition, but how can I return specific http code? I know I can send 404, but how I send 400 when the input arguments are invalid? Link to comment Share on other sites More sharing options...
wbmnfktr Posted December 30, 2021 Share Posted December 30, 2021 I'd like to expand that question to even more error-codes. What would be the best way to send as 30x, 40x, 50x as a response. There are some special codes I'd like to use, 307 for example. Link to comment Share on other sites More sharing options...
Robin S Posted December 30, 2021 Share Posted December 30, 2021 @Richard Jedlička @wbmnfktr You can set a response code in your hook like this: http_response_code(400); So you can set 400, 307, or whatever you need. https://www.php.net/manual/en/function.http-response-code.php 4 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted December 30, 2021 Share Posted December 30, 2021 For redirects I suggest: $session->redirect($url, 307); 3 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now