Symfony2. Problems with custom URLs. Urlencoding - url

I have following route in my bundle
/{category}
And I have category with name
Category/Brand
If url is something like this:
domain.com/Company/Brand
then I get error 500 about / symbol.
But if in twig I do
company.name|url_encode()
then I get
Company%2F%Brand
(Code might be wrong, dont remember right now)
But nevertheless Symfony tells me that there is no route matching
And gives me 404.
How can I solve this problem?

Are all your category names like that one or only some of them?
If all of them follow this structure, you could change the route to:
/{company}/{brand}
And change the corresponding controller to accept two variables instead of one. Later you can concatenate them or do whatever you need with them
If only some of them have this structure, you could try to replace the directory separator with some character combination in the controller which creates the link and then reverse this replacement in the controller for this route. For example, in the controller for the template where the link is shown you could
$nameEncoded = str_replace ('/','%%%%',$companyName);
pass this variable to the template and use it to generate the link and then in the receiving controller do:
$nameDecoded = str_replace ('%%%%','/',$companyName);

If your route is /{category} and the URL that you type is domain.com/Company/Brand, the error is normal.
You have to config your route in routing.yml like this:
Company/{category}

Ok!
I found a solution here.
#PUBLICATION URL
###_publication:
pattern: /{username}/{category}/{publicationid}
defaults: { _controller: ###:Default:publication }
requirements:
_method: GET
category: .+
username: .+
publicationid: \d+
#CATEGORY - should be at very end, to match all other URLS
###_category:
pattern: /{category}
defaults: { _controller: ###:Default:category }
requirements:
_method: GET
category: .+

Related

Drupal internalURL rewrite

I am trying to do something very simple, but can't seem to find a way to do it. What I want is to have a URL in the form of /en/products/accessories+software internally route to /en/products?accessories+software.
/en/products is an existing page, whatever comes after are basically filters, which are appended to the URL so they can be linked directly. Now it works by appending it as query parameters, but using /filtersHere would be preferred. But this of course results in a 404 error.
Site is running D9.
Why not use Drupal's Routing system and make dynamic arguments on URL?
In the module.routing.yml, something like below should do:
module.mypath:
# Dynamic arguments enclosed in { }.
path: '/book/export/{type}/{node}'
defaults:
_controller: '\Drupal\module\Controller\MyController::index'
More details available in the official doc:
Drupal - Structure of routes

How to use URL parameters with router in Vaadin Fusion?

I tried to follow the guide at
Question, which results in a
mobx.esm.js?4fd9:2362 [mobx] Encountered an uncaught exception that was thrown by a reaction or observer component, in: 'Reaction[MainView.update()]' TypeError: Expected "item" to be a string
My configuration is
path: 'item/:item',
component: 'item-view',
Is there an example, how to solve this? Do I need to handle this in the MainView (I follow the todo-tutorial on vaadin.com?
The problem is that main-view.ts in the starter project has logic which tries to list all navigation routes to generate links for them. Currently that logic is not smart enough to detect and skip routes that have non-optional route parameters.
The error is thrown from router.urlForPath(viewRoute.path) when the path has non-optional route parameters because here we are not specifying what the value for the route parameter should be (for the generated URL). For generating a URL for the path 'item/:item' it would need to do something like router.urlForPath('item/:item', { item: 'my-item' }).
The quick fix (to remove the title from the route definition) suggested by Marcus works because the main-view.ts has logic to skip all routes that don't have a title. You could change that logic to also skip by some other criteria or you could try to include values for the route parameters there (for specific routes) if you really want to generate working navigation links for those routes.
Other alternative would be to mark the route parameter as optional (in case you want the route also to be accessible without a parameter) by adding a question mark after it, then the link can be generated without specifying a value for the parameter.
{
path: 'item/:item?', // <- optional route parameter
component: 'item-view',
title: 'Hello World',
}
It seems you have run into an issue with the starter. In main-view.ts, it loops over the routes with a title and tries to create a link for each in the menu:
${this.getMenuRoutes().map(
(viewRoute) => html`
<vaadin-tab>
${viewRoute.title}
</vaadin-tab>
`
)}
However, router.urlForPath() breaks when there is a URL parameter definition instead of an actual parameter (items/:item instead of items/2).
A quick fix for this would be to remove the title property from your route configuration for any route that has a parameter.
{
path: 'item/:item',
component: 'item-view'
// make sure there is no title here
}

URL helpers can't recognize additional custom dynamic segments in routes

I am using Rails 4.2. In a gem, there is a route defined as
get 'book/:id', to: 'book#show', as: book
I'd like to add an additional dynamic segment in a custom route, so in the routes.rb file in my app, I have
get ':language/book/:id', to: 'book#show', as: language_book
Then in my controller, I tried to call the url helper
language_book_url language: :en, id: 3
I expect to get a url like http://host:port/en/book/3, however, instead, I get http://host:port/book/3?language=en. It seems I can't use the helper with that extra custom dynamic segment. Is it possible to get the desired path with the new dynamic segment variable? Thanks!
usmanali gave a answer in the comment, to use language_book_url :en, 3. But what if I want a mixture of dynamic segments and query string params? So my target url is http://host:port/en/book/3?barcode=1234. How can I call the url helper? A call to the helper like language_book_url('en', 3, barcode: 1234) would produce http://host:port/book/3?language=en&barcode=1234, instead of the expected one.

How to create nice path when routing from overview page to item page in angulardart?

I have a very common view structure with a list page and a details page. I want to create a very simple path structure like:
/leaderboard
/leaderboard/123
When setting up my routing i created:
..addRoute(
name: 'leaderboard',
path: '/leaderboard',
enter: view('view/leaderboards.html'))
..addRoute(
name: 'leaderboard',
path: '/leaderboard/:leaderboardId',
enter: view('view/leaderboard.html'));
When navigating to the /leaderboard everything works fine but when navigating to /leaderboard/123, angulardart complains that it has found multiple routes and doesn't route at all.
My current solution is to use a different path for the list page (/leaderboards with an 's' at the end) but that is not so nice.
As pointed in this answer of Angular Dart: matching rules for route path - implicit suffix wildcard? :
If you are worried about conflicts between two routes where path of one happens to be a prefix of another, then the correct approach is to put the most specific path first.
You can also consider using hierarchical routes.
Side note : it's recommended to have unique route names.
The accepted answer is correct, but I'm always nervous about using ordering to make the routes work correctly. I would recommend doing something like this:
..addRoute(
name: 'leaderboard',
path: '/leaderboard/all',
enter: view('view/leaderboards.html'))
..addRoute(
name: 'leaderboard',
path: '/leaderboard/:leaderboardId',
enter: view('view/leaderboard.html'));
Making the path something like 'leaderboard/all' (or something like that) makes it a little bit more robust, I believe.

how will you do this in MVC custom route?

www.yoursite.com/image/http://images.google.com.ph/images/nav_logo7.png
What I need to know here is the Controller Action, and the Global.asax routes
The colon : character is not valid in the path segment of the URL, so you'll have to either encode it, or remove it entirely. After that, you can use the {*routeValue} syntax to specify that the route value should be assigned the remainder of the URL.
routes.MapRoute(
"Image",
"image/{*url}",
new { controller = "Image", action = "Index" }
);
For the url http://www.yoursite.com/image/images.google.com.ph/images/nav_logo7.png
, the above route will execute ImageController.Index() with a url argument of "images.google.com.ph/images/nav_logo7.png". How you choose to deal with the protocol (encode/remove) is up to you.
Also keep in mind that a url authority can be made up of a domain name and a port number, separated by : (www.google.com:80) which would also need to be encoded.
If you want to send a URL as a parameter on a URL you need to URL Encode it first
In c# use Server.UrlEncode(string) from the System.Web namespace
So your example will look like:
www.yoursite.com/image/http%3a%2f%2fimages.google.com.ph%2fimages%2fnav_logo7.png
And your route pattern could be:
routes.MapRoute(
"image",
"image/{url}",
new { controller = "Image", action = "Index", url = "" }
);
I'd start by not trying to embed a second URL into your route.
In cases where I have to use a URL as part of a route, I replace the slashes with an alternate character so you don't have issues with the interpertation of the URL as a malformed route (i.e.~,|,etc.) then retranslate these with some string replaces in the controller. And if possible, I'd ditch the HTTP:// and assume the route is a URL by convention.
So your route would become something like:
www.yoursite.com/image/images.google.com.ph~images~nav_logo7.png
URL-encoded slash in URL - Stack Overflow
This is same problem and solved solutions.
1st solution.
Replace "://" to "/". Routing url pattern "image/{scheme}/{port}/{*url}".
2nd solution
"image/{*url}" set *url value base64.

Resources