daveh / php-mvc

A simple PHP model-view-controller framework, built step-by-step as part of the "Write PHP like a pro: build an MVC framework from scratch" course on Udemy.
https://davehollingworth.com/go/phpmvc/
MIT License
782 stars 311 forks source link

Problem with regex letters+numbers #111

Closed diogenesjup closed 1 year ago

diogenesjup commented 2 years ago

I'm trying to create a route using letters+numbers, but without success.

For example, the following route

$router->add('institucional/{slug:\w+}/', ['controller' => 'Institucional', 'action' => 'pagina']);

and then in my browser

/institutional/page

Works well.

But if I do:

/institutional/example-page-3 (letters+numbers)

does not work,

and the same goes for

/institutional/hiffens-example-page

It only works if I remove the dashes

How can I resolve this?

daveh commented 2 years ago

In addition to letters and numbers, "example-page-3" also contains hyphens. The \w metacharacter doesn't include hyphens, only letters, numbers and underscores. To fix it, you can create a character class that includes hyphen characters, e.g.

{slug:[\w-]+}

diogenesjup commented 1 year ago

Tks! Works fine with {slug:[\w-]+}/