Jump to content

Feature Request/Suggestion - URL Hooks ability to override URL Segments


JayGee
 Share

Recommended Posts

I absolutely love URL hooks and have used them extensively since they were introduced. I don't know if I'm alone in this, but the only thing that bugs me about how they work is when they conflict with URL segments in templates (especially if you have URL segments enabled on the home page, e.g in an SPA type scenario).

IMO it would be great if URL hooks could have an extra bool parameter to set whether or not they take precedence over URL segments. E.g.

$wire->addHook('/stripe/{route}', function($event) {

    switch ($event->route) {

    //CASES GO HERE
            
        default:
            throw new Wire404Exception();
            return;
    }
    
  }, true); //<------ option here

A number of times I've run into scenarios that require workarounds (or manually naming URL segments) to avoid conflict with home pages with URL segments which wouldn't be necessary if we could just instruct the hook to kick in before URL segments. Obviously this would need to be opt-in and at developer risk.

I hate to be that guy that's always tagging you into threads @ryan.... but I'm guessing I may be overlooking a logical reason why this is a bad idea or that this doesn't exist?!

  • Like 2
Link to comment
Share on other sites

I hit the same wall! 

It occurred to me to use the template's urlSegmentList not only as a whitelist, but also as a container for routable definitions, adding route definitions directly to the template configuration, which is intuitive and centralized.

According to the API documentation, the following lines are allowed as template-level URL segments, using the syntax regex: to notify PW that the "next" string is a regex.

'photos'
'photos/new'
'regex:^photos/photo[0-9]+$'
'regex:^export-(json|xml)$'

Following this, I tried to add a custom pattern and intercept the whitelist processing to control the process. I assume we are using custom pages to add new methods to the page.

method:path @handler

Example 1:

/*PATTERN*/
'get:/category/{id}/list @listCategory'

/*REQUEST*/
/category/321/list

/*RESPONSE*/
return page()->listCategory(id:321);

Example 2:

/*PATTERN*/
'get:/products/{name}/categories @categories\listAll'

/*REQUEST*/
/products/laptop/categories

/*RESPONSE*/
return pages('categories')->listAll(name:'laptop');

This allows me to define a complete set of routes based on the same template's urlSegmentList feature, like this:

'get:/{id}/list @listCategory'
'post:/add @addCategory'
'delete:/{id} @removeCategory'
'put:/{id} @updateCategory'

I implemented all the code for route processing and execution, etc., BUT... then I tried it as a Hook (with a lot of variations), as a Module, as a DefaultPage, etc., without success.

It's impossible to 'effectively' stop the super-powerful 404 page! Then I gave up. 

I think the only way is to modify the PW code ...or not?

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...