URL rules declared in terms of pattern-route pairs can cover the majority of projects. However, it is not flexible enough with dynamic data, where the URL could be any format and value stored in the database.
Now, we need to display item details using a URL that contains only the item title, such as http://hostname/basic/web/news/Test
news of 2015-04-19
There is no way to solve this with URL rules, as we have done until now.
A more general solution to parse and create URL requests is using Rule
classes.
The Rule
class extends Object
and implements UrlRuleInterface
.
The next example will explain how to display item details, finding it from the title (defined in data()
array of objects), and parsing and creating routes with a Rule
class.
The route displayed in the browser will have the news/title format.
For this purpose, create a new folder components
under the basic folder if it does not exist, and create components/NewsUrlRule.php
with the following content:
<?php namespace...