Gallery3:Developer Handbook:Controllers - Gallery Codex
Personal tools

Gallery3:Developer Handbook:Controllers

From Gallery Codex

Controllers

A typical controller is a class in modules/example/controllers. The URL to the controller corresponds to the controller class name and a function name inside the controller. For example if you’d like to create a controller for the following URL:

 http://example.com/gallery3/index.php/foo/bar

You would create a file called modules/example/controllers/foo.php containing this code:

 class foo_Controller extends Controller {
   public function bar() {
     print "Hello world";
   }
 }

When your module is installed and activated, the bar() function will respond to both GET and POST requests made to the /gallery3/index.php/foo/bar URL.

A typical controller would do more than just print out some text. It would probably load up one or more models and then delegate to a view to print out some HTML to render on the web page. For example:

 class foo_Controller extends Controller {
   public function bar() {
     $view = new View("foo_bar.html");
     $view->item = item::root();
     print $view;
   }
 }

This controller loads the root album and then looks for a view called foo_bar.html.php and loads it. When inside that view, the variable $item will be set to the root item. If in this example you want to print out the title of the root album, you could create modules/'example'/views/foo.html.php containing the following code:

 Gallery title: <?= $item->title ?>

If there are multiple views called foo.html.php the version chosen would be determined by the module load order.

Notes about controllers:

  • If your function name is index() then the URL is only the name of the controller. For example if foo_Controller() has function index(), then the route to that function is merely /gallery3/index.php/foo