How can I control the placement of URL parameters in my route URL? - asp.net-core

Suppose I'm creating a website to display customer orders. I want to have pages showing:
A list of customers, which allows me to drill down to...
A list of orders for a given customer, which allows me to drill down to...
Details of a particular order for the customer.
If I create a few Razor pages, I might end up with the following structure:
[Pages]
Index.cshtml
[Customer]
Index.cshtml
[Order]
Index.cshtml
The top-level page (list of customer) has no parameters, so the URL for that is just /.
The customer page (list of orders for a customer) needs a customer ID, so I can put #page "{customerId} at the top of the .cshtml file. It's URL will then be /Customer/123.
So far, so good. That URL makes sense.
Now for the order-detail page. This needs an order number, so let's put #page "{orderNo}" at the top of the .cshtml file. Its URL is... /Customer/Order/54321. Hmmm... I've lost the customer ID from the URL.
I can add the customer ID back in by making the #page directive include it (#page "{customerId}/{orderNo}"), but then the URL is /Customer/Order/123/54321 - which doesn't look good.
How could I get the URL to look like /Customer/123/Order/54321?

You can use an override route template to negate the path-based route that the framework generates. Override routes start with a leading /. In your example the solution is:
#page "/Customer/{customerid}/Order/{orderid}"
Note that this has the benefit of being robust in the face of changes to the structure of the site. You may, for example, decide in the future that nesting the Order folder within the Customer folder impedes discovery, but the override route will ensure that the page is found at the same URL regardless where you move the source files to within the Pages folder.

Related

Problem with id_product_attribute in URLs

I have a PrestaShop shop with a module for clean id from category urls. My url format for products is "{rewrite}-{id}.html" so PS add "?id_product_attibute=0" on all my products. I want to remove it or put my url format like "{rewrite}-{id}{-:id_product_attribute}.html"
I'm ussing https://github.com/faktiva/prestashop-clean-urls
The id_product_attribute can be removed from the Product URL without changing in the SEO configuration in the back-office.
You can override the getProductLink() function of the Link.php class or make changes in the CORE file.
Remove or comment the $params['id_product_attribute'] = $ipa; from line number 156 in the above function.

nuxtjs route map in order

I have an issues with nuxtjs route naming.
Ex: my pages folder is
page:
-|blog:
-|_categorySlug.vue
-|_postSlug.html.vue
-|service:
-|index.vue
When i access url like
example.com/blog/demo-post-slug.html -> route map to /blog/_categorySlug.vue
example.com/blog/demo-category-slug -> route map to /blog/_categorySlug.vue
Because _categorySlug.vue place in the first order inside dir blog.
When i change the file name _postSlug.html.vue to _aPostSlug.html.vue to make it become the first order.
page:
-|blog:
-|_aPostSlug.html.vue
-|_categorySlug.vue
-|service:
-|index.vue
It's all right but the file naming isn't good. Anyone can give me an suggestion for this?
Thanks!

Custom meta title on checkout/success page in opencart

I am new to opencart and need help with oc 1.5.5.1
I am trying to call custom meta title to add last order id on checkout/success page. I have followed a very helpfull response from shadyyx on Opencart successful order ID and Total from JavaScript
Now I am stuck at a point on how to use the order_id which is already available in session and call / display it in meta title of checkout/success page.
As I understood you, you need to check if the orderid exists and not empty and route is checkout/success so for example add this to $this->data['my-new-metatag']
All above do in the common/header controller (cos meta tags are in the common/header tpl file)
then in common/header.tpl check if $my-new-metatag exists, echo it, otherwise leave variable that echos by default

Completely custom path with YII?

I have various products with their own set paths. Eg:
electronics/mp3-players/sony-hg122
fitness/devices/gymboss
If want to be able to access URLs in this format. For example:
http://www.mysite.com/fitness/devices/gymboss
http://www.mysite.com/electronics/mp3-players/sony-hg122
My strategy was to override the "init" function of the SiteController in order to catch the paths and then direct it to my own implementation of a render function. However, this doesn't allow me to catch the path.
Am I going about it the wrong way? What would be the correct strategy to do this?
** EDIT **
I figure I have to make use of the URL manager. But how do I dynamically add path formats if they are all custom in a database?
Eskimo's setup is a good solid approach for most Yii systems. However, for yours, I would suggest creating a custom UrlRule to query your database:
http://www.yiiframework.com/doc/guide/1.1/en/topics.url#using-custom-url-rule-classes
Note: the URL rules are parsed on every single Yii request, so be careful in there. If you aren't efficient, you can rapidly slow down your site. By default rules are cached (if you have a cache setup), but I don't know if that applies to dynamic DB rules (I would think not).
In your URL manager (protected/config/main.php), Set urlFormat to path (and toptionally set showScriptName to false (this hides the index.php part of the URL))
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName'=>false,
Next, in your rules, you could setup something like:
catalogue/<category_url:.+>/<product_url:.+> => product/view,
So what this does is route and request with a structure like catalogue/electronics/ipods to the ProductController actionView. You can then access the category_url and product_url portions of the URL like so:
$_GET['category_url'];
$_GET['product_url'];
How this rule works is, any URL which starts with the word catalogue (directly after your domain name) which is followed by another word (category_url), and another word (product_url), will be directed to that controller/action.
You will notice that in my example I am preceding the category and product with the word catalogue. Obviously you could replace this with whatever you prefer or leave it out all together. The reason I have put it in is, consider the following URL:
http://mywebsite.com/site/about
If you left out the 'catalogue' portion of the URL and defined your rule only as:
<category_url:.+>/<product_url:.+> => product/view,
the URL Manager would see the site portion of the URL as the category_url value, and the about portion as the product_url. To prevent this you can either have the catalogue protion of the URL, or define rules for the non catalogue pages (ie; define a rule for site/about)
Rules are interpreted top to bottom, and only the first rule is matched. Obviously you can add as many rules as you need for as many different URL structures as you need.
I hope this gets you on the right path, feel free to comment with any questions or clarifications you need

Yii: CMenu items for different module

I'd like to make a menu in Layout which the items are linked to other different module.
e.g:
Item "Product" linked to an action in Product Module, item "Service" linked to an action in Service Module.
It won't work when I set the 'url'=>('product/<controllerID>/<actionID>') and 'url'=>('service/<controllerID>/<actionID>') because once we're in Product module and click the menu "Service", the URL become
index.php?r=product/service/<controllerID>/<actionID>
instead of
index.php?r=service/<controllerID>/<actionID>
and it will be 404 error. (for sure, because the Service Module isn't inside Product Module but the URL makes it looks like that).
Any solution for this?
Check the createUrl() documentation :
the URL route. This should be in the format of 'ControllerID/ActionID'. If the ControllerID is not present, the current controller ID will be prefixed to the route. If the route is empty, it is assumed to be the current action. If the controller belongs to a module, the module ID will be prefixed to the route. (If you do not want the module ID prefix, the route should start with a slash '/'.)
That last line tells us everything. Best thing to do for you is start all the routes with a / :
'url'=>array('/<moduleID>/<controllerID>/<actionID>')
Check this
'url'=>$this->createUrl('/<moduleId>/<controllerID>/<actionID>')