Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

Creating a Plugin and Can't Render View File Properly

I'm creating a simple plugin that is only going to display a few PHP pages with different paths, such as...

*domain.com/myplugin
*domain.com/myplugin/page1
*domain.com/myplugin/page2

The problem is that only the first page renders properly and everything else in the folder displays the theme with an empty page. Here is part of my code from class.myplugin.plugin.php...

public function pluginController_MyPlugin_create($sender, $args) {

      // Setup head.
      $sender->MasterView = '';
      $sender->removeCssFile('admin.css');
      $sender->addCssFile('style.css');
      $sender->CssClass = 'NoPanel';


      if ($args[0] != '') {
          $page = $args[0];
      } else {
          $page = null;
      }


      if($page == null){
        $sender->render('myplugin', '', 'plugins/MyPlugin');
      }
      else{
        if(file_exists('plugins/MyPlugin/views/' . $page . '.php')){
          // echo $page; (this actually echoes the page name properly above the theme, but the won't render the page with the code below)
          $sender->render($page, '', 'plugins/MyPlugin');
        }
         else{
          //would like to render a 404 page here if you know how
         }
     }
  }

The route is setup properly like so...

Route
^myplugin(/.*)?$

Target
plugin/myplugin$1

My folder structure is...

plugins/MyPlugin
plugins/MyPlugin/class.myplugin.plugin.php
plugins/MyPlugin/views
plugins/MyPlugin/views/myplugin.php
plugins/MyPlugin/views/page1.php
plugins/MyPlugin/views/page2.php

page1.php has unique content but if I go to domain.com/myplugin/page1, I only get the empty page with the theme. If I go to domain.com/myplugin, it will display the contents from myplugin.php just fine.

All the view files are similarly coded like this...

<?php defined('APPLICATION') or exit();
echo "unique content";
?>

Comments

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    You can do it like that:

        public function pluginController_dispatcher_create($sender, $args) {
            // "myplugin" in your case, but I found "index" better ;-)
            $page = val(0, $args, 'index');
            // Try to find a matching file in this plugin/views
            $view = $this->getView($page.'.php');
            // Check if this file exists and is readable
            if (!is_readable($view)) {
                // If not, throw default error message
                throw notFoundException();
            }
            // Show the requested view
            $sender->render($view);
        }
    
  • Options
    R_JR_J Ex-Fanboy Munich Admin
    edited January 2017

    If you need something more sophisticated, you can do it like it is shown in the official example plugin:

        public function pluginController_dispatcher_create($sender) {
            $this->dispatch($sender, $sender->RequestArgs);
        }
    
        // example.com/plugin/dispatcher or example.com/plugin/dispatcher/index
        public function controller_index($sender, $args) {
            $sender->render($this->getView('index.php'));
        }
    
        // example.com/plugin/dispatcher/page1
        public function controller_page1($sender, $args) {
            // do logic specific for page1
            $sender->render($this->getView('page1.php'));
        }
    
  • Options

    Thanks :) That really cleaned things up for me.

    Turns out I didn't have the proper permissions on the page1.php and page2.php files. The is_readable function tipped me off and now everything works perfectly!

Sign In or Register to comment.