Use Routing to rewrite URLs¶
This section will show you how you can rewrite the URLs for news using Routing Enhancers and Aspects. TYPO3 Explained has a chapter Introduction to routing that you can read if you are not familiar with the concept yet. You will no longer need third party extensions like RealURL or CoolUri to rewrite and beautify your URLs.
How to rewrite URLs with news parameters¶
On setting up your page you should already have created a site configuration. You can do this in the backend module Site Managements > Sites.
Your site configuration will be stored in
/config/sites/<your_identifier>/config.yaml
. The following
configurations have to be applied to this file.
Any URL parameters can be rewritten with the Routing Enhancers and Aspects.
These are added manually in the config.yaml
:
Add a section
routeEnhancers
, if one does not already exist.Choose an unique identifier for your Routing Enhancer. It doesn't have to match any extension key.
type
: For news, the Extbase Plugin Enhancer (Extbase
) is used.extension
: the extension key, converted toUpperCamelCase
.plugin
: the plugin name of news is just Pi1.After that you will configure individual routes and aspects depending on your use case.
/config/sites/<your_identifier>/config.yaml
¶1routeEnhancers:
2 News:
3 type: Extbase
4 extension: News
5 plugin: Pi1
6 # routes and aspects will follow here
Tip
If your routing doesn't work as expected, check the indentation of your configuration blocks. Proper indentation is crucial in YAML.
Using limitToPages¶
It is recommended to limit routeEnhancers
to the pages where they are needed.
This will speed up performance for building page routes of all other pages.
/config/sites/<your_identifier>/config.yaml
¶ 1routeEnhancers:
2 News:
3 type: Extbase
4 limitToPages:
5 - 8
6 - 10
7 - 11
8 extension: News
9 plugin: Pi1
10 # routes and aspects will follow here
Multiple routeEnhancers for news¶
If you use the news extension for different purposes on the same website (for example news and events), you may want different URL paths for them (for example /article/ and /event/). It is possible to configure more than one routing enhancer for the news plugin on the same website.
Use limitToPages
to assign the appropriate configuration to the
desired pages.
/config/sites/<your_identifier>/config.yaml
¶ 1routeEnhancers:
2 News:
3 type: Extbase
4 limitToPages:
5 - 8
6 - 10
7 - 11
8 extension: News
9 plugin: Pi1
10 # etc.
11 NewsEvents:
12 type: Extbase
13 limitToPages:
14 - 17
15 - 18
16 extension: News
17 plugin: Pi1
18 # etc.
About routes and aspects¶
In a nutshell:
routes
will extend an existing route (means: your domain and pagepath) with arguments from GET parameters, like the following controller/action pair of the news detail view.
aspects
can be used to modify these arguments. You could forexample map the title (or better: the optimized path segment) of the current news. Different types of Mappers and Modifiers are available, depending on the case.
URL of detail page without routing:
https://www.example.com/news/detail?tx_news_pi1[action]=detail&tx_news_pi1[controller]=News&tx_news_pi1[news]=5&cHash=
URL of detail page with routes:
https://www.example.com/news/detail/5?cHash=
URL of detail page with routes and aspects:
https://www.example.com/news/detail/title-of-news-article
The following example will only provide routing for the detail view:
/config/sites/<your_identifier>/config.yaml
¶ 1routeEnhancers:
2 News:
3 type: Extbase
4 extension: News
5 plugin: Pi1
6 routes:
7 - routePath: '/{news-title}'
8 _controller: 'News::detail'
9 _arguments:
10 news-title: news
11 aspects:
12 news-title:
13 type: NewsTitle
Please note the placeholder {news-title}
:
First, you assign the value of the news parameter (
tx_news_pi1[news]
) in_arguments
.Next, in
routePath
you add it to the existing route.Last, you use
aspects
to map thepath_segment
of the given argument.
Both routes and aspects are only available within the current Routing Enhancer.
The names of placeholders are freely selectable.
Common routeEnhancer configurations¶
Basic setup (including categories, tags and the RSS/Atom feed)¶
Prerequisites:
The plugins for List View and Detail View are on separate pages.
If you use the Category Menu or Tag List plugins to filter news records, their titles (slugs) are used.
Result:
Detail view:
https://www.example.com/news/detail/the-news-title
Pagination:
https://www.example.com/news/page-2
Category filter:
https://www.example.com/news/my-category
Tag filter:
https://www.example.com/news/my-tag
/config/sites/<your_identifier>/config.yaml
¶ 1routeEnhancers:
2 News:
3 type: Extbase
4 extension: News
5 plugin: Pi1
6 routes:
7 - routePath: '/'
8 _controller: 'News::list'
9 - routePath: '/page-{page}'
10 _controller: 'News::list'
11 _arguments:
12 page: 'currentPage'
13 - routePath: '/{news-title}'
14 _controller: 'News::detail'
15 _arguments:
16 news-title: news
17 - routePath: '/{category-name}'
18 _controller: 'News::list'
19 _arguments:
20 category-name: overwriteDemand/categories
21 - routePath: '/{tag-name}'
22 _controller: 'News::list'
23 _arguments:
24 tag-name: overwriteDemand/tags
25 defaultController: 'News::list'
26 defaults:
27 page: '0'
28 aspects:
29 news-title:
30 type: NewsTitle
31 page:
32 type: StaticRangeMapper
33 start: '1'
34 end: '100'
35 category-name:
36 type: NewsCategory
37 tag-name:
38 type: NewsTag
39 PageTypeSuffix:
40 type: PageType
41 map:
42 'feed.xml': 9818
43 'calendar.ical': 9819
Localized pagination¶
Prerequisites:
The website provides several frontend languages.
Result:
English:
https://www.example.com/news/page-2
Danish:
https://www.example.com/da/news/side-2
German:
https://www.example.com/de/news/seite-2
/config/sites/<your_identifier>/config.yaml
¶ 1routeEnhancers:
2 News:
3 type: Extbase
4 extension: News
5 plugin: Pi1
6 routes:
7 - routePath: '/{page-label}-{page}'
8 _controller: 'News::list'
9 _arguments: {'page': 'currentPage'}
10 defaultController: 'News::list'
11 defaults:
12 page: ''
13 requirements:
14 page: '\d+'
15 aspects:
16 page:
17 type: StaticRangeMapper
18 start: '1'
19 end: '100'
20 page-label:
21 type: LocaleModifier
22 default: 'page'
23 localeMap:
24 - locale: 'da_DK.*'
25 value: 'side'
26 - locale: 'de_DE.*'
27 value: 'seite'
Explanation:
The LocaleModifier
aspect type will set a default value for the
English language.
You're then able to add as many localeMap
configurations as you
need for the page translations of your website.
The value of locale
refers to the value in your site configuration.
Human readable dates¶
Prerequisites:
For List View with a Date Menu plugin, to filter by date. Also includes configuration for the pagination.
Result:
https://www.example.com/news/2018/march
https://www.example.com/news/2018/march/page-2
/config/sites/<your_identifier>/config.yaml
¶ 1routeEnhancers:
2 DateMenu:
3 type: Extbase
4 extension: News
5 plugin: Pi1
6 routes:
7 # Pagination:
8 - routePath: '/'
9 _controller: 'News::list'
10 - routePath: '/page-{page}'
11 _controller: 'News::list'
12 _arguments:
13 page: 'currentPage'
14 - routePath: '/{news-title}'
15 _controller: 'News::detail'
16 _arguments:
17 news-title: news
18 # Date year:
19 - routePath: '/{date-year}'
20 _controller: 'News::list'
21 _arguments:
22 date-month: 'overwriteDemand/month'
23 date-year: 'overwriteDemand/year'
24 page: 'currentPage'
25 # Date year + pagination:
26 - routePath: '/{date-year}/page-{page}'
27 _controller: 'News::list'
28 _arguments:
29 date-year: 'overwriteDemand/year'
30 page: 'currentPage'
31 # Date year/month:
32 - routePath: '/{date-year}/{date-month}'
33 _controller: 'News::list'
34 _arguments:
35 date-month: 'overwriteDemand/month'
36 date-year: 'overwriteDemand/year'
37 page: 'currentPage'
38 # Date year/month + pagination:
39 - routePath: '/{date-year}/{date-month}/page-{page}'
40 _controller: 'News::list'
41 _arguments:
42 date-month: 'overwriteDemand/month'
43 date-year: 'overwriteDemand/year'
44 page: 'currentPage'
45 defaultController: 'News::list'
46 defaults:
47 page: '0'
48 date-month: ''
49 date-year: ''
50 requirements:
51 date-month: '\d+'
52 date-year: '\d+'
53 page: '\d+'
54 aspects:
55 news-title:
56 type: NewsTitle
57 page:
58 type: StaticRangeMapper
59 start: '1'
60 end: '25'
61 date-month:
62 type: StaticValueMapper
63 map:
64 january: '01'
65 february: '02'
66 march: '03'
67 april: '04'
68 may: '05'
69 june: '06'
70 july: '07'
71 august: '08'
72 september: '09'
73 october: '10'
74 november: '11'
75 december: '12'
76 date-year:
77 type: StaticRangeMapper
78 start: '2000'
79 end: '2030'
Explanation:
You will need a new routePath
for every possible combination of
arguments (pagination, month with/without pagination, ...).
Potential errors:
If you want 2018/march
but get 2018/3
instead, compare your
StaticValueMapper
for months with your date arguments.
Are you using different date formats (with/without leading zeros)?
You can either remove the leading zero in your aspects
or adapt the
TypoScript setting:
1plugin.tx_news.settings.link {
2 hrDate = 1
3 hrDate {
4 day = j
5 // 'n' for 1 through 12. 'm' for 01 through 12.
6 month = m
7 year = Y
8 }
9}
You can configure each argument (day/month/year) separately by using the configuration of PHP function date.
Warning
Using the StaticRangeMapper
is strictly limited to 1000 items per
a single range and 10000 items per routing enhancer.
That means you'll have to multiply all possible combinations in a routing enhancer, for example:
12 months × 30 years (2000-2030) × 25 pages (pagination) = 9000 possible items
If you exceed this limit, you'll either have to build a custom and more
specific mapper, or reduce the range in one of your StaticRangeMapper
.
How to create URLs in PHP¶
The following snippet is a good example how an URL can be generated properly
1protected function generateUrl(SiteInterface $site, int $recordId, int $detailPageId): string
2 {
3 $additionalQueryParams = [
4 'tx_news_pi1' => [
5 'action' => 'detail',
6 'controller' => 'News',
7 'news' => $recordId
8 ]
9 ];
10 return (string)$site->getRouter()->generateUri(
11 (string)$detailPageId,
12 $additionalQueryParams
13 );
14 }