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
You can do it like that:
If you need something more sophisticated, you can do it like it is shown in the official example plugin:
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!