phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


Joomla - Component: Generating Friendly Links

Tag: joomla, cms, Joomla Component   Category:
post: 12 Nov 2007 read: 3,982


You know, standard url at Joomla like this http://jomla-site/index.php?option=com_contact&task=view&id=123. It appear as long GET string. For your visitors and search engine, it doen't make happy.

It will be very helpful if our like like this, http://jomla-site/contact/view/123. Do you agree? This tutorial focus how to generate friendly links.

This tutorial is series from Joomla! tutorial before. So, please read Joomla! tutorial before.

  1. Login as administrator, at http://localhost/joomla/administrator.
  2. Open Global Configuration by click Site > Global Configuration.
  3. Set Search Engine Friendly Urls to "yes" and Use Apache mod_rewrite to "yes".

    mod_rewrite must be installed in your Apache. If you don't know about how to install module mod_rewrite, you can read tutorial about mod_rewrite in this site (see category: Apache).

  4. Change "htaccess.txt" within joomla directory to be ".htaccess".
  5. Create "router.php" within joomla/components/com_hello. Enter following code:
    
    <?php
    defined( '_JEXEC' ) or die( 'Restricted access' );
    
    function HelloBuildRoute(&$query)
    {
      $segments = array();
    
      if(isset($query['task'])) 
      {
        $segments[] = $query['task'];		
        unset($query['task']);
      };
    	
      if(isset($query['id']))
      {
        $segments[] = $query['id'];
        unset($query['id']);
      };
    
      return $segments;
    }
    ?>
    
  6. Now, open "hello.html.php". Update showhello() with following code:
    
    <?
    	function showHello($rows, $option)
    	{ ?>
      <table>
      <?php
      foreach($rows as $row)
      {
    
        $link = JRoute::_( 'index.php?option='.$option.'&id='.$row->id . '&task=view' );
    
        echo '
        <tr>
          <td>
          <a href="'. $link .'">'. $row->message .'</a>
          </td>
        </tr>
        ';
      }
    	?>
      </table>
      <?php
    	}
    ?>
    
  7. Change following line code at viewHello():
    
    <?php $link = JRoute::_('index.php?option='. $option); ?>
    

    with this:

    
        <?php $link = JRoute::_('index.php?option='. $option); ?>
    
  8. Point your browser to http://localhost/joomla. We look how router works. Look all links change like this:
    
    http://localhost/joomla/hello-world
    http://localhost/joomla/news-feed
    http://localhost/joomla/web-links
    
  9. Open hello world at http://localhost/joomla/hello-world
  10. Click one of items. You will get error. We must intepret url before.
  11. Open again router.php. Add following bold code:
    
    <?php
    defined( '_JEXEC' ) or die( 'Restricted access' );
    
    function HelloBuildRoute(&$query)
    {
      $segments = array();
    
      if(isset($query['task'])) 
      {
        $segments[] = $query['task'];		
        unset($query['task']);
      };
    	
      if(isset($query['id']))
      {
        $segments[] = $query['id'];
        unset($query['id']);
      };
    
      return $segments;
    }
    
    function HelloParseRoute($segments)
    {
      $vars = array();
      $vars['task']	= $segments[0];
      $vars['id']		= $segments[1];
      return $vars;
    }
    
    ?>
    

Finish, urls like this must work:


http://localhost/joomla/hello-world/view/1
http://localhost/joomla/hello-world/view/2
http://localhost/joomla/hello-world/view/3
...


Series this article:
Joomla - Component: Create Hello World Component on Front End
Joomla - Component: Create Hello World Component on Back End
Joomla - Component: Registering Your Component in database
Joomla - Component: Create Component Link for Front End
Joomla - Component: Creating Toolbars
Joomla - Component: Available Toolbar Buttons
Joomla - Component: Creating the Database Table
Joomla - Component: Creating Form Input Data at Back End
Joomla - Component: Saving Data to Database
Joomla - Component: Creating a List Screen
Joomla - Component: Creating Advance List Screen
Joomla - Component: Editing Data
Joomla - Component: Deleting Data
Joomla - Component: Updating publish state
Joomla - Component: Hello From Database at Front End
Joomla - Component: a Hello page
Joomla - Component: Generating Friendly Links
Joomla - Component: Adding Comment ( Create Table )
Joomla - Component: Adding Comment (Create Form)
Joomla - Component: Adding Comment ( Insert Data )
Joomla - Component: Adding Comment ( Displaying Comments )
Joomla: Fast Road to Understand Component Programming

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Joomla: Fast Road to Understand Component Programming
Chart: How to Build Cool Animation Real Time Chart
Email: Send Attachement Mail
SMS : Sending SMS with PHP and ActiveXperts (Part 1)

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


624
posting