Welcome Guest, Not a member yet? Register   Sign In
CI Portlet - XPortlet
#1

[eluser]XMadMax[/eluser]
XPortlets for Codeigniter 2.


=========================================================================
LAST UPDATE: v.0.3.
Demo version working in CI 2.0.2.
Framework included in download:

Download from Googlecode: http://code.google.com/p/xportlet/

=========================================================================
Call a controller from any template, allowing a resusable template, template compositions, reducing code in main controllers, and have specific controllers for any small functionality.

Any controller can have it's own models, views, and be inserted in any other view.
Similar to 'widtgets' in Yii, extends it to allow any controller to be called by Ajax, and same code to be inserted in views, without any modification.

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* XPortlet Library
*
* Call a controller from any template, allowing a resusable template, template compositions, reducing
* code in main controllers, and have specific controllers for any small functionality.
*  
* Install this file as application/libraries/XPortlet.php
*
* Autoload it in config/autoload.php:     $autoload['libraries'] = array('XPortlet');
*
* Add a portlet call to any template:
* <?php XPortlet::run('ControllerName', 'method', param1, param2, ...); ?>
*
* @version:     0.2
* $copyright    Copyright (c) Xavier PĂ©rez 2011-03-18
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

class XPortlet
{
    /* Define modulesPath if you use Modular MVC */
    static $modulesPath = "";
    
    /**
     * function run
     *
     * @param $className
     *
     * Can be a class, or directory and class as :  admin/Login
     *  
     */
    
    function run($className, $methodName = "")
    {
        $className = ucfirst($className);
        $basePath = APPPATH.'controllers/';
        
        if (self::$modulesPath != "")
        {
            $basePath = APPPATH.self::$modulesPath."/".$className."/controllers/";
            $baseNames = explode("/",trim($className,"/"));
            $className = $baseNames[count($baseNames)-1];
        }
        
        if (file_exists($basePath.$className.EXT))
        {
            $args = func_get_args();

            require_once $basePath.$className.EXT;
            
            if (isset($this->$className) && is_object($this->$className))
                $class = $this->$className;
            else
                $class = new $className();
                
            $method = $args[1];
            
            if ($methodName == '')
                $methodName = "index";

            $segments = array(); $segments[] = "";
            foreach(array_merge(array($className),array_slice($args, 2)) as $key => $val)
            {
                $segments[] = $this->uri->_filter_uri($val);
            }

            $this->uri->segments = $segments;
            
               return call_user_func_array(array(&$class, $methodName), array_slice($args, 2));
        }
    }
}

Install

1) Copy XPortlet.php to your libraries directory
2) Add XPorlet to your autoload libraries: $autoload['libraries'] = array('XPortlet');


Test:

Create a controller and it's view (example: TestPorlet.php).

Edit any other view (example: welcome.php), and put anywhere:

Code:
<?php XPorlet::run('TestPorlet'); ?>

Call the welcome controller:
http://yourdomain.com/welcome

You will see the TestPorlet results inside the welcome view.



Using parameters:

Code:
<?php XPorlet::run('TestPorlet','YourMethodHere'); ?>
<?php XPorlet::run('TestPorlet','YourMethodHere','Param1'); ?>
<?php XPorlet::run('TestPorlet','YourMethodHere','Param1','Param2'); ?>
<?php XPorlet::run('TestPorlet','YourMethodHere','Param1','Param2','Param3'); ?>

Other uses:

You can add a route to config/routes.php to all direct call:

http://www.yourdomain.com/testPortlet/in...am2/param3


Considerations:

All controller files must to be named MixedCase
All controller classes must to be defined MixedCase
All module directories (modular MVC), must to be MixedCase
#2

[eluser]XMadMax[/eluser]
Updated text, modified source.
#3

[eluser]XMadMax[/eluser]
=========================================================================
LAST UPDATE: v.0.3.
Demo version working in CI 2.0.2.
Framework included in download:

Download from Googlecode: http://code.google.com/p/xportlet/

=========================================================================




Theme © iAndrew 2016 - Forum software by © MyBB