Symfony2 routing - slug with slashes and pagination - symfony

I use Symfony2 and have structure of categories and subcategories with articles in them. In the lists I use pagination.
If I call a URL like 'category1-slug/4' it works fine and opens a page 4 of the list with articles in Category 1.
But if I want to use a slug like "category1/subcategory3/4", it returns 404, because it takes the whole string as slug - "category1/subcategory3/4".
How should I configure my routing? I want the last digit to be taken for page number. My current definition is:
app_article_category:
path: /articles/{slug}{trailingSlash}{page}
defaults: { _controller: CmsArticleBundle:AppArticle:showItem, page: 1, trailingSlash:"/"}
requirements:
page: \d+
trailingSlash : "[/]{0,1}"
slug: .*$

adding a new route, before the existing one, should do the job :
app_article_category_subcategory:
path: /articles/{slug_cat}{trailingSlash}{slug_subcat}{trailingSlash}{page}
defaults: { _controller: CmsArticleBundle:AppArticle:showItem, page: 1, trailingSlash:"/"}
requirements:
page: \d+
trailingSlash : "[/]{0,1}"
slug_cat: .*$
slug_cat: .*$

Related

Speaking urls in Ext:news with multiple plugins

I have a problem with the site configuration of news when there are two news plugins on one page (TYPO3 v9.5.20). There is a list view and a tag list as navigation for the tags ([screenshot1][1]). In the list view I am able to click on tags and only news with these tags are displayed. This works and the URLs fit. So everything is ok.
But if I activate the tag list plugin I get an error ([screenshot2][2]): The controller "News" is not allowed by plugin "Pi1". Please check for TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin() in your ext_localconf.php.
The site configuration in config.yaml looks like this:
routeEnhancers:
News:
type: Extbase
limitToPages:
- 141
- 142
extension: News
plugin: Pi1
routes:
- routePath: 'page/{page}'
_controller: 'News::list'
_arguments:
page: '#widget_0/currentPage'
- routePath: '/{news-title}'
_controller: 'News::detail'
_arguments:
news-title: news
- routePath: '/{category-name}'
_controller: 'News::list'
_arguments:
category-name: overwriteDemand/categories
- routePath: '/{tag-name}'
_controller: 'News::list'
_arguments:
tag-name: overwriteDemand/tags
defaultController: 'News::list'
defaults:
page: '0'
aspects:
news-title:
type: PersistedAliasMapper
tableName: tx_news_domain_model_news
routeFieldName: path_segment
page:
type: StaticRangeMapper
start: '1'
end: '100'
category-name:
type: PersistedAliasMapper
tableName: sys_category
routeFieldName: slug
tag-name:
type: PersistedAliasMapper
tableName: tx_news_domain_model_tag
routeFieldName: slug
[1]: https://i.stack.imgur.com/LsFux.png
[2]: https://i.stack.imgur.com/H2IZn.png

Optional text on Symfony router

I'm trying something with the Symfony router but I cannot achieve what I'm aiming to.
Indeed I want to add an optional text to this path, especially for the /page-{page}. So I'm asking if it's possible to have two possibilities for the same router path, like the following ones:
/topic/{id}
/topic/{id}/page-{page}
routing.yml:
utm_forum_forum:
path: /forum/{id}/page-{page}
defaults:
_controller: UTMForumBundle:Forum:forum
page: 1
requirements:
id: \d+
page: \d+
Thanks for reading, and if I'm not clear enough I can re-explain :)
You need to include routes for all possible configurations.
So, if your page slug is optional, your route table should look something like:
utm_forum_forum_default:
path: /forum/{id}
defaults:
_controller: UTMForumBundle:Forum:forum
page: 1
requirements:
id: \d+
utm_forum_forum:
path: /forum/{id}/page-{page}
defaults:
_controller: UTMForumBundle:Forum:forum
requirements:
id: \d+
page: \d+
That should provide a default value of page 1 when the page slug is omitted entirely, or require something in the form page-N after the ID.
utm_forum_forum:
path: /forum/{id}/page-{page}
defaults: { _controller: UTMForumBundle:Forum:forum, page: null }
so if you request for /forum/15/page- will give you null in {page} parameter.
Don't know if its what you need

How to Hide parameters in URL in Symfony2

I don't want the user see the parameters in my URL.
Example :
my_route:
path: /***/{id}
defaults: { _controller: MyBundle:Default:myaction}
the route will be generated like this :
***/product/1
i want just see the
***/product
As explained in comments, the request needs something to identify the resource you want to fetch. But this has nothing to do with URL rewriting. If you want to display the slug of the resource instead of the id, do the following :
my_route:
path: /products/{slug}
defaults: { _controller: MyBundle:Default:myaction}
Then, in MyBundle/Controller/DefaultController.php :
public function myactionAction(Request $request, $slug)
{
$product = $this->getDoctrine()->getManager()->getRepository('MyBundle:Product')->findOneBy(["slug" => $slug]);
}
and when you want to create a link to this product, use
link to the product

Symfony: Routing issue add/ {id}/edit

I am having an issue with my Routing.
When I try and visit domain.com/listing/add I get the error below
Parameter "id" for route "listing_edit" must match "[^/]++" ("" given) to generate a corresponding URL.
I understand with the edit route it will require domain.com/listing/1/edit but I thought having the listing/add route above the edit route I should still be able to visit domain.com/listing/add.
What am I doing wrong?
route.yml
listing_add:
pattern: listing/add
defaults: { _controller: Bundle:Listing:add }
listing_edit:
pattern: listing/{id}/edit
defaults: { _controller: Bundle:Listing:edit}
If you are using twig, you must use these kind of links:
YOUR LINK TO ADD
YOUR LINK TO EDIT

Symfony2 Routing with Folders (containing multiple forward slashes)

I am looking to setup some routes in Symfony2, but I am struggling to setup a dynamic route for folders.
I am trying to setup a route that accepts the following: /department/sub-department/sub-sub-department/product-url.html
From that route all I need is the product-url and the rest is more for SEO. The problem I have is that a route may have many department levels in the URL, so I need to ignore everything before the product-url.
It seems like the "/" is the problem here, so is there a way to escape the slashes.
If I don't use any of the departments in the routing I can use this:
product:
pattern: /{url}.html
defaults: { _controller: CompanyBundle:System:pageRequest }
So, I basically need something like this:
product:
pattern: /{department}/{url}.html
defaults: { _controller: CompanyBundle:System:pageRequest }
Where the {department} can be one or more departments with forward slashes in.
Is that possible at all?
There's a nice article about it in the cookbook:
You must explicitly allow / to be part of your parameter by specifying a more permissive regex pattern.
In your case the route definition would have to be
product:
pattern: /{department}/{url}.html
defaults: { _controller: CompanyBundle:System:pageRequest }
requirements:
department: ".+"
product:
pattern: /{url}.html
defaults: { _controller: CompanyBundle:System:pageRequest, department: ~ }
product_department:
pattern: /{department}/{url}.html
defaults: { _controller: CompanyBundle:System:pageRequest }
requirements:
department: '[\w\d\/\-]+'

Resources