Need Joomla 2.5 Custom Component Router.php Explanation - php

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.

Related

Remove specific parameters from URL while leaving others intact

I have a few filters on my view which a user can select. Let's say in this example it is a webshop and the user is viewing a page to buy t-shirts. On the sidebar the user can select a few parameters which will be added to the URL:
https://www.myshop.com/shirts?size=22&gender=male&somethingelse=true
Now when the user clicks on view all sizes, I want the size=22 to be removed from the URL while keeping the other parameters intact:
https://www.myshop.com/shirts?gender=male&somethingelse=true
I found somewhere to do that, I have to do the following in my blade file (Mind you I need to do this from the blade view, not from any controller or helper method):
View all sizes
But this does not work, it keeps the size=22 in the URL. What am I missing here, or is there perhaps a different approach to this?
Edit: Added dump of request()
In my example I am using a webshop but in my actual site I am using date and category, but the base functionality is the same.
In the end, this did work after all:
View all sizes
I think I did something wrong in the beginning. After revisiting this solution, it worked.

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();

Yii urlManager username in URL

I have url, localhost/user/about/id/5, i want it to convert to a like localhost/john.doe/about, is it possible to do it in Yii?
john.doe refers to username
about refers to action
i want to hide controller name, in this case user
Thanks for the help
Yes you'll need to use a custom UrlRule as in the docs here (Using Custom Url Classes). You can then strip apart the URL in your class, try and find a user name, if it doesn't exists simply return false and let the rest of the URL rules process.
Bear in mind the higher up the order of URL rules you place your custom one, the more often it will be run (as UrlManager will exit on the first matching rule) so it has performance implications if you just put it right at the top.
Bonus
This will also help you in generating URLs as you can just pass a user name as a parameter to a normal URL and have your class do the complicated bit.

PHP MVC: How to dispaly content via text not id values through the URL

I am used to creating small php mvc structures etc and relying on passing the id of the item i need to display:
www.asite.co.uk/controller/method/IDnumber
But I see a lot of sites use say the title of a page within the URL.
www.asite.co.uk/controller/this-is-a-title
www.asite.co.uk/controller/i-am-another-section/this-is-a-title
MY question is its easy to get the contents via the ID method but who would you work it to use the text method to retrieve the data / content you require.
Initially I would think of removing the '-' separators then searching the 'title' field say for the result, but not sure if there would be a better/easier way.
Many Thanks
I would like to tell you that the text in url is just name of the page. As I also work in MVC so i know that it is only a page name, the requested data is in post method or stored in sessions which are taken forward to the page and after they are made in use, they are destroyed.
I use a column in my table content called "slug" and find the content searching by this column. This column needs to be unique, of course.
If you just want to display header differently, do it via htaccess:
http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

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