Documentation:Plugins

From TinyMVC Documentation

Jump to: navigation, search

Contents

What is a plugin?

Plugins are how you extend the functionality of the framework. The two currently available plugin types are libraries and scripts. Libraries contain a PHP class that gets instantiated, whereas scripts are just PHP scripts that get included.

Learning by example

Loading a library

Plugin files live in the /myapp/plugins/ directory. Lets create a library plugin called demo:

/myapp/
  /plugins/
    tinymvc_library_demo.php

The file name and class name must match. The filename must be all lower-case, but the class name can be any case.

tinymvc_library_demo.php

class TinyMVC_Library_Demo {
  function test()
  {
    return "This is a test.";
  }
}


To use our new plugin, we load it in the controller.

/myapp/controller/hello.php

class Hello_Controller extends TinyMVC_Controller
{
  function index()
  {
    $this->load->library('demo','mydemo');
    $this->view->assign('output', $this->mydemo->test());
    $this->view->display('hello_view');
  }
}

The second parameter is the alias for the property name, which is optional.

You should now have a variable $output in your view with the demo test output.

Loading a script

Lets create a script plugin called myhelpers:

/myapp/
  /plugins/
    tinymvc_script_myhelpers.php

The script name must be lower-case.

tinymvc_script_myhelpers.php

function esc($string)
{
  return htmlentities($string);
}
function anchor($url,$text)
{
  return "<a href=\"$url\">$text</a>";
}


To use our new plugin, we load it in the controller.

/myapp/controller/hello.php

class Hello_Controller extends TinyMVC_Controller
{
  function index()
  {
    $this->load->script('myhelpers');
    $this->view->display('hello_view');
  }
}

The PHP functions in the script are now available to your view files.

Libraries and scripts can optionally be autoloaded. See Autoloading plugins.

Personal tools