PHP Symfony duplicate routes - php

How can I create duplicate routes?
I want to have SEO and user friendly routing like:
/{product.slug}
/{category.slug}
But how does Symfony know which one to render? It now defaults to the last definition.

You can create you route with two variables:
/{type}/{slug}
type would be productor category
Or, if you want only one variable, you can adapt your function depending on the value passed by URL.

Related

Optimization suggestions for Laravel routing

I'm creating a Laravel 5.1 application that lists products on pages. Now each product on the database has the url of the product detail page. I have done a method that exports all those urls and convert's them to laravel routes, and the routes are written into a file and included on the laravel routing. I have done that on that way in order to be able to optimize the routing using laravel's routes:cache command. Now my question in fact is about optimization, which way would be better, to have a file with all routes, let's say 100K routes or to have a single entrance point that compares the route in question inside the database and return the respective product.
There isn't a need to have an individual route for each product. Possibly Store a unique slug(Semantic URL) in the database? Then have one route that displays a product based on a what is passed to the route.
user friendly slug could be something like 't-shirt-9000'. If you don't want this, you can always use the unique id you already have set for the product in the database.
Within your show method, you would need to query the DB with the slug past in the request to the database.
quick example:
In routes.php
Route::get('/products/{product}', 'ProductController#show');
Product Controller
public function show($product)
{
$productRequested = Product::where('url_slug', $product)->first();
// Do whatever from here
}
the fact that you use a framework you will be enforced to optimize your code
route file allow you to get the fastest way to minimize the number of mapped URLs.
the notion of route offered by Symfony to laravel allow you to manipulate all variables of your url with one single route , for example :
route::get('{category}/{id}/{slug}/{var1}/{var2}/{var3}/{varX}','yourController#yourMethod');
this route will send all variables in the url to yourMethod();

Laravel: How to get the current url as a view helper with parameters

I want to build a link in my view that refers to the same page like that one where its placed on. And I want to be able to give a parameter with.
For example I want to change languages. I have a route like
domain.com/{lang}/xyz
And in my view I want to do something like
EN
So I can easily reload the page but just change the "lang" parameter.
Hopefully its understandable. Please try to help me.
(Another side question: Are there no ressources eg a list of all view helpers in Laravel? where do i know which viewhelpers are available?)
Use laravel's helper method to use in a view:
url()->current()
This will get the current URL. If you need to get the current route name,
Route::current()->getName()
Now you can use this route name to create your own new URL.
eg:
EN
Your route definition may be something like:
Route::get('/{lang}/about/', ['as'=>'about_us', 'uses'=>'PagesController#about'])
This will provide you the current URL.
But in your case, it's better to use the this package for multi language:
https://github.com/mcamara/laravel-localization
It's pretty simple and easy to use.

Need Joomla 2.5 Custom Component Router.php Explanation

I'm completely lost on how to use the routing capabilities of Joomla for a custom component.
Here is an example link that is being sent to the custom component through a form submission:
http://superiordash.com/products/dash-kits/index.html?make_id=8&model_id=6&year_id=48&option=com_mmg&view=products&page=list&sel_make_id=-1&sel_model_id=-1&sel_year_id=-1&Itemid=580
What I want to do is grab the make_id, model_id, and year_id, find the corresponding values in the database and use them for the url as such: products/dash-kits/YEAR-MAKE-MODEL.html
So for the example link above:
http://superiordash.com/products/dash-kits/2002-acura-rsx.html
Any ideas from the greats out there?
Also, from what I understand from the documentation - Am I going to have to find a way to explode the rest of the query or it will be attached at the end of url? And if so - is there a way to explode every part of the query except year, make, and model, no matter what it is?
Any help would be most grateful. I've been searching for solutions, half solutions etc. and will continue to do so.
It's actually quite easy. You will need to use 2 functions in your router.php:
ComponentNameBuildRoute
and
ComponentNameParseRoute
The build route will return an array containing the elements to be displayed in the URL. For example, for a link that is http://www.test.com/products/category-name/product-name, the build route function will return array('category-name', 'product-name'); . The array is passed to a Joomla function automatically to generate the SEF URL.
The parse route function will translate the SEF URL into a GET URL. The function will receive $segments as a parameter, and that segments will consist of the portions in the URL, such as category-name and product-name. In that function, you will need to get the ID of the category and the product, and return an array that is something like array('category_id'=>$category_id, 'product_id'=>$product_id);
You can check the router.php for the banners component (which is the simplest component) and use it as a startup point.

Loading controller from the URL Parameters or URL-Routing or _remap

I am new to CodeIgniter and wanna implement something like the below example. In the below example, Adidas is a brand name, Casual-Shoes is the type of shoes, Winterwear, Summerwear are some other types.
http://www.myntra.com/winterwear
http://www.myntra.com/adidas
http://www.myntra.com/casual-shoes/
http://www.myntra.com/sales
and I know that winterwear, Adidas, casual-shoes are not the controller names or method names. In my project, I wanna implement something like this where index file will find out whether Adidas is a brand name, or winterwear is type of season and if found, then load respective controller by passing the arguments.
I think, this is the part of URL Routing or checking db and then, get the controller or method name. Even, Breadcrumbs should work like this?
Any help with the code example would be appreciated.
Above thing can be done using _remap function/method. You can read more about it from here - http://www.web-and-development.com/codeigniter-remove-index-php-minimize-url/

Symfony - Need a form filter to accept GET variable OR other method to create a filter link

Using Symfony 1.3
I have a normal form-filter that is used to filter values of a list,
it works normally. What I'd like to add is a link that is outside of the
form that can be used to filter by just a single criteria.
Anybody have a solution to this? Is there a way to set a filter to accept a GET?
You can approach this in 2 ways:
setting filters based on get parameters: symfony - admin module filters accessible as links
setting table method based on get parameters: Symfony doctrine admin generator list filters get method with no csrf token
I prefer second approach, because it gives you possibility to use filters on top your filtered list.
I resolved this in a much easier fashion than i suspected. The request already accepts GETS.
I just created a link with the parameters set as array values.
echo link_to('link_text', 'module/filter', array('query_string' => 'module_filters[field][text]='.$object->getField(ESC_RAW)))
Clicking on that link does exactly what I need.

Categories