简体   繁体   中英

Using Regular Expressions in a PHP Framework

I've got a university project to create a site using a framework given to us by the teacher. I've hosted the site at http://daniel-lucas.com so you can see what's happening.

As you can see the address ends with /?index.

In the configuration file I've just used :

new UrlMapping('^index/$', 'musiconlineapp.listeners.MainListener.handle', 'musiconlineapp/views/MainView.php')

The first parameter is the address to map. The second is the controller for the page and the method to load in the controller (handle). The third is the view to display for the page.

However I'm having trouble displaying categories for the site. I was told to use mapping like this:

new UrlMapping('^category/(?P<cat>\w+)/$', 'musiconlineapp.listeners.CategoryTitleListener.handle', 'musiconlineapp/views/CategoryTitleView.php' )

My problem is that I've not done any regex for a while and can't figure out what address I'm supposed to go to when I click on the link. I've tried /?category&cat=rock and similar variations but none of them work.

That regex looks like it's supposed to match /?category/rock/ or the like.

^           # beginning of string
category/   # literal 'category/'
(?P<cat>    # named matching group (named 'cat')
\w+         # one or more word characters a-zA-Z0-9_
)           # end the named matching group
/           # literal '/'
$           # end of string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM