Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to set patterns for wildcards in annotations? #59

Open
vesper8 opened this issue Mar 24, 2017 · 5 comments
Open

how to set patterns for wildcards in annotations? #59

vesper8 opened this issue Mar 24, 2017 · 5 comments

Comments

@vesper8
Copy link
Contributor

vesper8 commented Mar 24, 2017

I'm using many wildcards and getting some mismatching which would be solved if patterns were respected

for example I have these two routes

/{any}/{page}
and
/{any}/{name}

going to /something/12 should hit the first route
and going to /something/name should hit the 2nd route

regardless of what order they are defined in

I tried adding

Route::pattern('page', '[0-9]+');

to the RouteServiceProvider

which is supposed to enforce that the {page} argument must always be fully numerical in order for that route to be matched.. but it's not working

Do you know if using annotations for my routes with this package is ignoring patterns set in the RouteServiceProvider?

And is there a way then to set regex patterns for wildcard routes directly in the annotations?

Or another solution?

Many thanks!

@vesper8
Copy link
Contributor Author

vesper8 commented Mar 24, 2017

managed to the get patterns so I know they're not being ignored, but would still like to know if I can set a pattern via annotation

@tshafer
Copy link
Contributor

tshafer commented Feb 5, 2018

Im not sure if this is possible. Ill dig into it soon.

@alexfoo
Copy link
Contributor

alexfoo commented Apr 14, 2019

I managed to get it working like this after #83:

/**
 * @Get("moo/{id}", as="mooInt")
 * @Where(id="[0-9]*")
 */
public function mooInt(Request $request, $id)
{
	return ["int" => $id];
}

/**
 * @Get("moo/{name}", as="mooStr")
 * @Where(name="[a-zA-Z-_]*")
 */
public function mooStr(Request $request, $name)
{
	return ["str" => $name];
}

Tested like this:

$ curl localhost:8000/moo/name -H 'Accept: application/json' -s
{
  "str": "name"
}
$ curl localhost:8000/moo/123 -H 'Accept: application/json' -s
{
  "int": "123"
}

But the where parameter should work like this even before the patch:

@Get("moo/{id}", as="mooInt", where={"id": "[0-9]*"})

@vesper8
Copy link
Contributor Author

vesper8 commented Dec 4, 2020

@alexfoo thanks a lot for your reply!

Would you happen to know how I can set it so that it would match anything except a short list of reserved words?

Meaning, I have this route annotation

    /**
     * @Get("/{room}")
     */

And I want this route to match every word, alpha or numeric or whatever, except for a small list of these reserved routes: nova, telescope, horizon, log-viewer

I'm really bad a regex and can't seem to figure this one out

@vesper8
Copy link
Contributor Author

vesper8 commented Dec 4, 2020

Figured it out!

    /**
     * @Get("/{room}")
     * @Where(room="^((?!\b(nova|horizon|telescope|log-viewer)\b).)*")
     */

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants