[Plugin] URL Router

A place to share your Plugins and Extensions

[Plugin] URL Router

Postby raymond » Tue Jan 04, 2011 2:36 pm

URL Router Plugin
----------------------------------
The URL Router plugin is used to map friendly MVC-style URLs to a Raxan page view. This is accomplished by using special segments within the url path:

URL format:

http://domain.com/page/view
http://domain.com/page/view/param
http://domain.com/page/view/param1/param2/paramN

Here's a break down of the above url format:

page - This represents the name of the web page (without the extension)
view - This represents the page view name to be loaded
param1...paramN - Parameters to be passed to the view.

URL Examples:

http://domain.com/admin/login - will be mapped to http://domain.com/admin.php?vu=login
http://domain.com/customer/edit/23 - will be mapped to http://domain.com/customer.php?vu=edit/23;
http://domain.com/customer/query/mary?age=25 - will be mapped to http://domain.com/customer.php?vu=query/mary&age=25;

Sample PHP Code:

class CustomerPage extends RaxanWebPage {

protected function _queryView() {
$param1 = $this->get->textVal(0);
$param2 = $this->get->textVal(1);
$param3 = $this->get->intVal(2);
// do something here
}
}

Installation
To use the url router plugin download the attached zip file and follow the steps below:

1. Copy url.router.php to raxan/plugins
2. Copy the files index.php and router.htaccess files to your web application root.
3. Rename router.htaccess to .htaccess

Now you're ready to start using friendly url's within your application or web site.

Note: The URL Router plugin requires a web server (e.g. apache) with mod_rewrite or compatible features enabled.

Additional plugin method

* createUrl() - Returns an MVC-style url if friendly url is supported
* createLink() - Returns hyperlink html
* redirectToView()- Redirects the browser to a new pageview
Attachments
url.router.zip
URL Router Plugin (zip)
(2.84 KiB) Downloaded 221 times
raymond
Site Admin
 
Posts: 215
Joined: Tue Mar 17, 2009 5:04 am

complex usage of router

Postby finaxis » Fri Sep 28, 2012 10:45 am

Hello, Dear Raymond !

router works great. Since I changed my dir structure, can't make it work. I've been fiddling with router since quite some hours but can't make it to work. Please help decode the mystery.

Code: Select all
//site
 //   - raxan
 //   - app
 //       - app1
 //       - app2
 //   - index.php
 
 class site_Index_Php extends RaxanWebPage
{
            function _init()
            {
                if($jsenabled) // ignore checkjs code
                {
                    $this->redirectToView('index');  //else checkjsview
                }
            }
           
            function _indexView()
            {
                $refer = explode('.', $_SERVER['HTTP_host']); //intended to use with sub-domain
      $host = $refer[1]; //  mapping with folder name like app1
          $url = config('site.host') . config('site.url') . 'app/' . 'app1';
      $this->redirectTo($url);
            } // redirects to app1/index.php
}

// app1
    //- index.php
    //- home.php
    //- member.php
    //- loggedout.php
   
//  app1/Index.php
    $start = Raxan::loadPlugin('init');
    $start->init();

// init plugin
//var_dump(Raxan::config('site.host').Raxan::config('site.url')); outputs :: http://localhost/site/app/app1/
function init()
    {
        $router = Raxan::loadPlugin('url.router');
                                                                           
        foreach (Raxan::config('routes') as $k => $v)      //$config['routes] = array('route.' => 'home','route.logout' => 'logout')
        {
                $k = str_replace('route.', '', $k);
                $router->addRoute('/' . $k, '/' . $v);
        }
        $router->initRouter();
        return $router;
    } //loads app1/home.php


//   app1/home.php extends RaxanWebPage
function _indexView()
    {
        $this->redirectTo('loggedout') ; //app1/loggedout.php   
    }

    // BROWSER ERROR - The page isn't redirecting properly
    //Firefox has detected that the server is redirecting the request for this address in a way that will never complete.



intended usage (obviously)
http://localhost/site/app/app1/home/view1/param1 or http://localhost/sites/app/app1/loogedout

Thanks and Regards,
Finaxis
finaxis
 
Posts: 28
Joined: Thu Feb 16, 2012 1:17 pm

Re: [Plugin] URL Router

Postby raymond » Sun Oct 21, 2012 8:37 pm

Hi Finaxis,

You can accomplish this by adding the .htaccess file to the app1 and app2 folders. Check out the attached example

__
Raymond
Attachments
example.zip
(4.83 KiB) Downloaded 54 times
raymond
Site Admin
 
Posts: 215
Joined: Tue Mar 17, 2009 5:04 am

Re: [Plugin] URL Router

Postby finaxis » Fri Nov 16, 2012 8:22 am

Yes, it works perfect even after removing the 'app' folder. now I am using :
Root
->raxan
->app1
----->htaccess for app1
----->index.php for app1
->app2
----->htaccess for app2
----->index.php for app2
index.php for root
htaccess for root.

Your trouble shoot is just hitting a bulls eye. No doubt.
Cheers !
finaxis
 
Posts: 28
Joined: Thu Feb 16, 2012 1:17 pm


Return to Plugins & Widgets

Who is online

Users browsing this forum: No registered users and 1 guest

cron