Documentation:Plugins

From TinyMVC

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/
    library.demo.php

The library names can be any case, but the class name must match the library name.

library.demo.php

class 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/
    script.myhelpers.php

The script names can be any case.

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