how to generate url friendly in Joomla? - php

I have a problem with joomla 1.5 friendly url (that not so friendly actually)
I am not using SEF at the moment (should i ?)
heres is my problem
I have some categories and sections. Each has alias.
so i can check all news category for example by visiting www.myxyz.com/news/
to check an article the url that generated would become:
www.myxyz.com/news/10-local-news-title-alias
I have no idea how joomla generate that url. In my templates i need to generate some links to specific articles.
so I create a helper in template:
// helper to get alias in mainMenu ... alias must be unique
function getMainMenu($menuAlias){
$items = &JSite::getMenu();
// Get Menu Items
$rows = $items->getItems('alias', $menuAlias);
if($rows){
//$result = JRoute::_(JURI::base().$rows[0]->link);
$result= JURI::base().substr(JRoute::_($rows[0]->link), strlen(JURI::base(true)) + 1);
return $result;
}else{
return JURI::base() ;// aka not found
}
}
but when I enter the page like www.myabc.com/news/7-local-news-alias
the url would become messed up and changed to wrong url.
should I use SEF for joomla url friendly ?

You should just work with normal url's in your code. when you turn on SEF joomla! will automaticly convert all links you create to SEF urls and when a request comes in it will revert them back to regular url's for you...

It's a little convoluted, but the proper way to link to SEF URLs is to use the original, non-SEF link. You will need:
The ID of the article
The Itemid of the menu item for your section (e.g. the menu item linking to Article Blog layout, etc)
Then you just link to:
index.php?option=com_content&view=article&id=42&Itemid=3
Where 42 is the article ID and 3 is the menu ID.Your link will then look something like:
/section-alias/42-the-article-alias
If you miss off the Itemid your link will look like this (I think):
/components/content/42-the-article-alias

Related

e-commerce href management

I have a question regarding e-commerce websites. Lets assume there is an e-commerce website that sells products for women, men, kids. For each of these categories it sells clothes, shoes ---etc. When a user clicks on women or men or whatever category, the user should be directed to a page showing the list of products corresponding to that category. Lets assume that the website have the same layout. For example the navbars, the squares where products will be displayed are the same. the only difference is the content it self. My question does large e-commerce websites have a separate html file for each product or they have some a template html file where the design and layout is fixed and based on link clicked the content of html is changed. Having a separate html file would be very cumbersome. If having a template html file is the case, how does the name of the link in browser change when different products are clicked is it related to .htaccess.
Thanks.
Yes .htaccess is important in this case. Try to learn how to parse url, and make some simple MVC applications.
There are many methods to doing this. The main answer to your question is that you need to parse the url. I'll go over a couple here.
1) PHP looks like it would be the easiest solution if you're connecting to a mysql database with products. You can create a php file with the categories as parameters, so each link could look like products.php?category=shoes
Then you can parse that url with $_GET in your code.
$category = $_GET['category'];
Then, in your php file, you can display specific content based on the category like so:
if ($_GET['category'] = 'shoes'){
//then enter your logic here
}
elseif ($_GET['category'] = 'books'){
//then enter your logic here
}
Here's some more info on the PHP GET: http://w3schools.sinsixx.com/php/php_get.asp.htm
You can do this via javascript by parsing the href
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var dynamicContent = getParameterByName('dc');
and then writing the logic to correctly create elements that fit your requirements through javascript.
More details: http://jennamolby.com/how-to-display-dynamic-content-on-a-page-using-url-parameters/
This should help you move forward to understanding how to achieve your concept hopefully!

Wordpress dynamic url for same page template

I have created page template in wordpress. right now its url is static like http://localhost/wordpress/test-test1 but i want to make it dynamic like http://localhost/wordpress/test1-test-test2 . Is it possible to create a single page template having dynamic url??
I want both url's to comes on single page. If page templating approach is not good then what could be other approaches. Here the test urls:
http://localhost/wordpress/test-test1
http://localhost/wordpress/test1-test-test2
http://localhost/wordpress/test13-test2-test1
I found similar links but none of the link helped me.
Here it depends on how would you change to pass argument, Suppose the last stage and it should be same always, and let page name 'anything'
<?php
add_action('init', 'add_my_rule');
function add_my_rule()
{
global $wp;
$wp->add_query_var('args');
add_rewrite_rule('test\/laststage\/(.*)','index.php?pagename=Pagename&args=$matches[1]','top');
Apply your custom template to this page, & on that template use this:
//if you visit http://.../test/laststage/name/AnyName, thus this $params will be name/AnyName.Now need to explode this and get the value.
$params = get_query_var('args');
Here's info: http://www.workingwith.me.uk/articles/scripting/mod_rewrite

session variable that records an onclick element

Lets say i have 5 pages which has some hierarchy and all pages are in same folder. When i click on one page its going to another page in same folder.
Example
fruits.php is main page and banana.php, apple.php, orange.php & pineapple.php. All the later 4 has links in main page. I need to display navigation in all the pages like if its main page just main page. If its a banana i should display as Main_Page>>Fruits>>Banana ans so on how can i achieve if i have n number of hierarchies lets say flowers(lily.php, rose.php), Eatables(chocolates.php, biscuits.php) etc and all pages in same folder.
make folders foreach category and have them in the main folder:
main/flowers
main/fruits
...
include php files in respective folders:
main/flowers/lily.php
main/fruits/apple.php
...
now use javascript to read the url of the current page:
var url = window.location;
var cat = url.split("/");
cat[cat.length - 1] = cat[cat.length - 1].split(".")[0]; // to remove ".php" part from "lily.php" and get "lily" to display
now you have those names in cat array, so you can display them in html as you need:
alert(cat[1]+"/"+cat[2]); // should popup something like fruits/Apple
First, what you are trying to make is called "breadcrumb". If you google it in this words you'll have too many hits.
There are too many ways to achieve this according to your site scheme. If you have a collection of your pages in your DB you can find the hierarchy of pages to show in breadcrumb by querying the DB. If you don't have this, I can advise you to make a folder hierarchy as:
Main
|--fruits.php
|--flowers.php
|--Fruits
|--apples.php
|--bananas.php
|--Flowers
|--lily.php
|--rose.php
Then on each page you can show your folder hierarchy as breadcrumb. This may be an easy way to start.

Different urls generated with JRoute::_() in different pages - Joomla! 3

I developed a component in Joomla! 3, and I used JRoute::_() & router.php to make urls search-engine-friendly. something like this :
/component/products/WIFI-IP-Phone/list-3
So I decided to replace /component/products with a clean alias, And I created a menu with a clean alias to the component home page. now, all the link I have inside the component ( generated with JRoute::_() ) are like this : /escene/WIFI-IP-Phone/list-3 and its perfect, its exactly what I want, But ... I'm using JRoute::_() in three different modules, and I generate links with that, the problem is that generated links in these modules when I'm in the home page or any other page except the component pages, are different with the generated links in these module when I'm in the component pages.
When I'm in the home page or other : /component/products/WIFI-IP-Phone/list-3
When I'm in my component pages : /escene/WIFI-IP-Phone/list-3
Any body can explain the reason Or help me to make all urls like /escene/WIFI-IP-Phone/list-3 ??
This is because the functions you write in components router.php get executed for the links when the page displaying is of the same component. But there is a way to accomplish this task.
1. First create a new menu in the menu manager and create all links in this menu.
2. Publish this menu but do not assign any position.
3. In this way you would get a sef url for each link.
if(JFactory::getConfig()->get('sef')) {
echo 'My sef url';
} else {
echo 'Dynamic url';
}
In this way Joomla will do your url parsing by detecting your component through the alias stored.
Let me know if you have any further query.

Joomla 3.2 get Itemid for component view

I have a menu link that links to a certain page on my component, entering this page will have the correct Itemid, the problem is that (I will give the same example as the joomla webiste) this view displays a list of puppies, but when I enter detailed view of a puppie I loose the Itemid.
I have a specific Itemid, that I want to assign to this detailed view, the menu is already created and assigned to the view with an XML file, now what I need is the best way to assign the Itemid to the links on the listing view...
This is the closest thing Joomla Docs seems to have http://docs.joomla.org/Supporting_SEF_URLs_in_your_component
Any ideas?
I found an answer, you can force an Itemid on the onbeforeread on the controller
function onAfterRoute() {
$jinput = JFactory::getApplication()->input;
$jinput->set('option', 'com_helloworld');
$jinput->set('view', 'helloview');
$jinput->set('Itemid', '200');
}

Categories