I have created a library that which will load a php file (which may contains users custom functions in it...) you can either call it from bootstrap also from the controller. if file does not exists it will display the error msg. Thing is am i doing it in the currect way?
If i did miss any thing point me out.. Thanks
Helpers is the folder where users can put php files
app/
controllers/
models/
helpers/
library/
views/
In "library/" Folder a php file named "helperfile.php"
class helperfile extends Phalcon\Mvc\User\Component
{
var $helper_Folder = '../app/helpers';
var $files = array();
public function __construct()
{
}
public function initialize()
{
}
public function include_file($files, $run = true)
{
if (!is_array($files))
$files = array($files);
foreach ($files as $file)
$this->files[$file] = $file;
if ($run)
$this->load();
}
public function beforeDispatch()
{
$this->load();
}
private function load()
{
if (empty($this->files))
return false;
foreach ($this->files as $file) {
$file = trim($file) . '.php';
if ($this->is_file_exists($file)) {
require $this->helper_Folder . '/' . $file;
}
}
}
private function is_file_exists($path)
{
$full_path = $this->helper_Folder . '/' . $path;
if (!file_exists($full_path)) {
$this->flash->error("Helper File Missing: " . $full_path);
return false;
}
return true;
}
}
// to auto load file on every page through the bootstrap ("public/index.php")
$di->set('dispatcher', function () {
//Create/Get an EventManager
$eventsManager = new Phalcon\Events\Manager();
/*
* Load Custom function files which are in the helpers folder
*/
$loadHelper = new helperfile();
$loadHelper->include_file([
'calling_from_bootstrap_1',
'calling_from_bootstrap_2'
],false);
$eventsManager->attach('dispatch', $loadHelper);
$dispatcher = new Phalcon\Mvc\Dispatcher();
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});
// To load it from controller
$loadHelper = new helperfile();
$loadHelper->include_file([
'calling_from_theController'
]);
That looks like it shoould work, but I think you underestimate the amount of work Phalcon can do for you.
An example of what is in the helper files would be useful. For the sake of this example I will assume that that it is like this:
app/
helpers/
ProductHelper.php
and in ProductHelper.php
class ProductHelper{
// code here
}
In your bootstrap where you have the loader you define your directories
$phalconLoader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$phalconLoader->registerDirs(
array(
$phalconConfig->application->controllersDir,
$phalconConfig->application->modelsDir,
// path to helper dir here
)
)->register();
and then in your controller
public function productAction(){
$productHelper = new productHelper();
}
That should work. It is less code, so is simpler should run a bit faster (using phalcon's built in code rather than writing some php will always be faster)
If the code in the helpers is not in classes, or not named the same as the filename, then it probably should be. Makes things a lot simpler.
Di Enabled Version
class ProductHelper extends \Phalcon\DI\Injectable{
public $config;
public function myFunction(){
$this->config = $this->getDI ()->get ('config');
}
}
and in the controller
public function indexAction()
{
$helper = new ProductHelper();
$helper->setDI($this->getDI());
$helper->myFunction();
}
Alternatively when creating your DI
$di->set ('productHelper', function () use ($config, $di) {
$helper = new ProductHelper();
$helper->setDi ($di);
return $helper;
});
and in the controller
public function indexAction()
{
$helper = new ProductHelper();
$helper->myFunction();
}
Related
I am makeing a small framework for scheduled job that are run by a nodejs external process. I would like to use an auto loader but for some reason data is not getting to it. I am also using namespaces. Here is what my folder structure looks like:
Library
|_ Core
|_ JobConfig.php
|_ Utilities
|_ Loader.php
|_ Scheduled
|_ SomeJob.php
|_ Config.php
My Config.php just has some definitions and an instance of my Loader.php.
Loader.php looks like:
public function __constructor()
{
spl_autoload_register(array($this, 'coreLoader'));
spl_autoload_register(array($this, 'utilitiesLoader'));
}
private function coreLoader($class)
{
echo $class;
return true;
}
private function utilitiesLoader($lass)
{
echo $class;
return true;
}
So for my SomeJob.php I am including Config.php and then try to instantiate JobConfig.php when it fails. My namespaces look like Library\Core\JobConfig and so on. I am not sure if this is the best approach without the ability to bootsrapt things. But I am not seeing the echo's from the loader before it fails.
Edit:
I tried the sugestion by #Layne but did not work. I am still getting a class not found and seems like the class in not getting in the spl stack. Here is a link to the code
If you actually use namespaces in the same way you're using your directory structure, this should be rather easy.
<?php
namespace Library {
spl_autoload_register('\Library\Autoloader::default_autoloader');
class Autoloader {
public static function default_autoloader($class) {
$class = ltrim($class, '\\');
$file = __DIR__ . '/../';
if ($lastNsPos = strrpos($class, '\\')) {
$namespace = substr($class, 0, $lastNsPos);
$class = substr($class, $lastNsPos + 1);
$file .= str_replace('\\', '/', $namespace) . '/';
}
$file .= $class . '.php';
include $file;
}
}
}
Put that into your Library directory and require it on a higher level. Hopefully I didn't mess that one up, didn't test it.
EDIT: Fixed path.
use this class to index of your project, just replace WP_CONTENT_DIR with another directory level to scan php files, this class automatically include files:
<?php
class autoloader
{
public static function instance()
{
static $instance = false;
if( $instance === false )
{
// Late static binding
$instance = new static();
}
return $instance;
}
/**
* #param $dir_level directory level is for file searching
* #param $php_files_json_name name of the file who all the PHP files will be stored inside it
*/
private function export_php_files($dir_level, $php_files_json_name)
{
$filePaths = array(mktime());
/**Get all files and directories using iterator.*/
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir_level));
foreach ($iterator as $path) {
if (is_string(strval($path)) and pathinfo($path, PATHINFO_EXTENSION) == 'php') {
$filePaths[] = strval($path);
}
}
/**Encode and save php files dir in a local json file */
$fileOpen = fopen($dir_level . DIRECTORY_SEPARATOR . $php_files_json_name, 'w');
fwrite($fileOpen, json_encode($filePaths));
fclose($fileOpen);
}
/**
* #param $php_files_json_address json file contains all the PHP files inside it
* #param $class_file_name name of the class that was taken from #spl_autoload_register_register plus .php extension
* #return bool Succeeding end of work
*/
private function include_matching_files($php_files_json_address, $class_file_name)
{
static $files;
$inc_is_done = false;
if ($files == null) {
$files = json_decode(file_get_contents($php_files_json_address), false);
}
/**Include matching files here.*/
foreach ($files as $path) {
if (stripos($path, $class_file_name) !== false) {
require_once $path;
$inc_is_done = true;
}
}
return $inc_is_done;
}
/**
* #param $dir_level directory level is for file searching
* #param $class_name name of the class that was taken from #spl_autoload_register
* #param bool $try_for_new_files Try again to include new files, that this feature is #true in development mode
* it will renew including file each time after every 30 seconds #see $refresh_time.
* #return bool Succeeding end of work
*/
public function request_system_files($dir_level, $class_name, $try_for_new_files = false)
{
$php_files_json = 'phpfiles.json';
$php_files_json_address = $dir_level . DIRECTORY_SEPARATOR . $php_files_json;
$class_file_name = $class_name . '.php';
$files_refresh_time = 30;
/**Include required php files.*/
if (is_file($php_files_json_address)) {
$last_update = json_decode(file_get_contents($php_files_json_address), false)[0];
if ((mktime() - intval($last_update)) < $files_refresh_time || !$try_for_new_files) {
return $this->include_matching_files($php_files_json_address, $class_file_name);
}
}
$this->export_php_files($dir_level, $php_files_json);
return $this->include_matching_files($php_files_json_address, $class_file_name);
}
/**
* Make constructor private, so nobody can call "new Class".
*/
private function __construct()
{
}
/**
* Make clone magic method private, so nobody can clone instance.
*/
private function __clone()
{
}
/**
* Make sleep magic method private, so nobody can serialize instance.
*/
private function __sleep()
{
}
/**
* Make wakeup magic method private, so nobody can unserialize instance.
*/
private function __wakeup()
{
}
}
/**
* Register autoloader.
*/
try {
spl_autoload_register(function ($className) {
$autoloader = autoloader::instance();
return $autoloader->request_system_files(WP_CONTENT_DIR, $className, true);
});
} catch (Exception $e) {
var_dump($e);
die;
}
I'm trying to make a prototype of a simple plugin system I plan on implementing into one of my projects. I have 4 files:
Index.php
Plugins/__BASE.php
Plugins/Sample.php
The index file checks if the 'oncall' method belongs to a class in the Plugins folder using functions I defined in the Plugins class (__BASE.php). If it does exist, it will execute it.
require_once 'Plugins/__BASE.PHP';
$func = 'oncall';
$plugins = new Plugins();
if($plugins->IsPluginMethod($func)) {
$obj = $plugins->GetObject($func);
call_user_func(array($obj, $func));
}
else
echo "'$func' isn't part of a plugin!";
__BASE.php is the base plugin class which all of the plugins will extend. It has two methods: IsPluginMethod() and GetObject(). IsPluginMethod checks if the method name supplied belongs to a class and GetObject returns an instance of the class the method belongs to.
class Plugins {
public $age = "100";
public function IsPluginMethod($func) {
foreach(glob('*.php') as, $file) {
if($file != '__BASE.php') {
require_once $file;
$class = basename($file, '.php');
if(class_exists($class)) {
$obj = new $class;
if(method_exists($obj, $func))
return true;
else
return false;
}
}
}
}
public function GetObject($func) {
foreach(glob('*.php') as $file) {
if($file != '__BASE.php') {
require_once $file;
$class = basename($file, '.php');
if(class_exists($class)) {
$obj = new $class;
return $obj;
}
}
}
}
}
Sample.php is a sample plugin which prints $this->age which is defined in the Plugins class.
class Sample extends Plugins {
public function oncall() {
echo "Age: {$this->age}";
}
}
This is what I see in index.php:
'oncall' isn't part of a plugin!
Can anyone help? Thanks.
In __BASE.php file change (glob('*.php') to glob('Plugins/*.php')
I´m making a tiny MVC framework for learning purpose.
I have a file and a class called load which is in my model folder. It reads the requested file name and checks if the file is in the views folder and returns the correct file.
The problem I´m having is that I´m trying to make a handling for bad urls so that if the requested file/url does not excist you are directed to the index.php page in the views folder...
I have an if statement inside the function that checks if the file exists and I thought I could just write an else statement requiring the index.php file incase the file was not found...
But this doesn´t work. All I´m getting is a white blank page when I type in an non existing page even I´f I try echo something in the else statement...
Does anyone know what´s missing or what I´m doing wrong?
UPDATE:
Added mainController class
This is what the hole load class looks like:
<?php
/* model/load.php
*/
class load
{
/* This function takes parameter
* $file_name and match with file in views.
*/
function view($file_name, $data = null)
{
if (is_readable('views/' . $file_name)) {
if (is_array($data)) {
extract($data);
}
require 'views/' . $file_name;
} else {
//This is where I thought I could require the index.php file...
}
}
}
And in my controller folder I have a mainController class sending the files to the load file.
This is what the mainController class looks like:
<?php
/* controller/main.php
*
*/
class mainController
{
public $load;
public $urlValues;
public function __construct()
{
$url = parse_url($_SERVER['REQUEST_URI']);
$url = explode('/', trim($url['path'], '/'));
$this->urlValues = array('controller' => $url[1]);
//Index page
if ($this->urlValues['controller'] == "index.php") {
$key = array("key" => "");
$this->load = new load();
$this->load->view('index.php', $key);
}
//Register page
if ($this->urlValues['controller'] == "register.php") {
$this->load = new load();
$this->load->view('register.php');
}
//Home page
if ($this->urlValues['controller'] == "home.php") {
$this->load = new load();
$this->load->view('home.php');
}
}
}
It looks as though you are only calling the view method when the controller matches, and therefore it never gets executed. Try something like this, and work from there:
<?php
/* controller/main.php
*
*/
class mainController
{
public $load;
public $urlValues;
public function __construct()
{
$url = parse_url($_SERVER['REQUEST_URI']);
$url = explode('/', trim($url['path'], '/'));
$this->urlValues = array('controller' => $url[1]);
// ifs go here
$this->load = new load();
$this->load->view($this->urlValues['controller']);
}
}
I am using a Spotify library called MetaTune and was able to do this easily in CodeIgniter but with Yii there have been some teething issues however currently it has started saying:
Fatal error: Call to undefined method stdClass::searchTrack() in ....public_html/Yii/news/protected/controllers/NewsController.php on line 67
Howevever, the the function is there. The files in this library all have a .class.php suffix (e.g. MetaTune.class.php) and the libray files are all stored in:
yii/application/protected/vendors/Metatune
With Codeigniter I made an additional spotify.php outside of the folder and autoloaded that to my controller, but im not sure if this is necessary.
I have loaded it in my config.php with:
'import'=>array(
'application.models.*',
'application.components.*',
'application.vendors.metatune.*',
),
Here is the Controller code:
public function actionView($id)
{
$model=$this->loadModel($id);
$spotify = MetaTune::getSomething();
$hello = $model->title;
Yii::import('application.vendors.metatune.MetaTune');
$spotify->autoAddTracksToPlayButton = true; // Will add all searches for tracks into a list.
$spotify->playButtonHeight = 330; // For viewing the entire playlist
$spotify->playButtonTheme = "dark"; // Changing theme
$spotify->playButtonView = "coverart"; // Changing view
try
{
$tracks = $spotify->searchTrack($hello);
$tracks = $spotify->getPlayButtonAutoGenerated($hello);
}
catch (MetaTuneException $ex)
{
die("<pre>Error\n" . $ex . "</pre>");
}
$song = 'tracks';
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
Please also see the code below where it has a function called getInstance which doesnt work well with Yii for some reason and Im not sure if I can change this as I used this to import MetaTune into the CodeIgniter controller without any issues.
Just a part of the MetaTune.class.php code:
Yii::import('application.vendors.metatune.Artist');
Yii::import('application.vendors.metatune.Album');
Yii::import('application.vendors.metatune.Track');
Yii::import('application.vendors.metatune.CacheRequest');
Yii::import('application.vendors.metatune.MBSimpleXMLElement');
Yii::import('application.vendors.metatune.SpotifyItem');
Yii::import('application.vendors.metatune.MetaTuneException');
....
class MetaTune {
const CACHE_DIR = 'application/vendors/metatune/cache/'; // Cache directory (must be writable) relative to this file
const USE_CACHE = false; // Should caching be activated?
const CACHE_PREFIX = "METATUNE_CACHE_"; // prefix for cache-files.
const SERVICE_BASE_URL_SEARCH = "http://ws.spotify.com/search/1/";
const SERVICE_BASE_URL_LOOKUP = "http://ws.spotify.com/lookup/1/";
const PLAYBUTTON_BASE_URL = "https://embed.spotify.com/?uri=";
public $autoAddTracksToPlayButton = false;
private $list = array();
// Holds instance
private static $instance;
.....
public static function getSomething()
{
if (!isset(self::$instance))
{
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}
.....
public function searchTrack($name, $page = 1)
{
$url = self::SERVICE_BASE_URL_SEARCH . "track?q=" . $this->translateString($name) .
$this->addPageSuffix($page);
$contents = $this->requestContent($url);
$xml = new MBSimpleXMLElement($contents);
$tracks = array();
foreach ($xml->track as $track)
{
$tracks[] = $this->extractTrackInfo($track);
}
if ($this->autoAddTracksToPlayButton) {
$this->appendTracksToTrackList($tracks);
}
return $tracks;
}
If you have any suggestions I would be most grateful. Thanks.
You didn't initialize $spotify anywhere, and php made it into stdClass by default, since you were assigning values like to member properties using that variable, but it failed when you tried calling unexisting method on it.
Solution: initialise it before you use it
$spotify = MetaTune::getInstance();
Well, is there something like before() method in kostache module? For example, if I have a couple of PHP lines inside of the view file, I'd like to execute them separately inside of the view class, without echoing anything in the template itself. How can I handle that?
You can put this type of code in the constructor of your View class. When the view is instantiated, the code will run.
Here is a (slightly modified) example from a working application. This example illustrates a ViewModel that lets you change which mustache file is being used as the site's main layout. In the constructor, it chooses a default layout, which you can override if needed.
Controller:
class Controller_Pages extends Controller
{
public function action_show()
{
$current_page = Model_Page::factory($this->request->param('name'));
if ($current_page == NULL) {
throw new HTTP_Exception_404('Page not found: :page',
array(':page' => $this->request->param('name')));
}
$view = new View_Page;
$view->page_content = $current_page->Content;
$view->title = $current_page->Title;
if (isset($current_page->Layout) && $current_page->Layout !== 'default') {
$view->setLayout($current_page->Layout);
}
$this->response->body($view->render());
}
}
ViewModel:
class View_Page
{
public $title;
public $page_content;
public static $default_layout = 'mytemplate';
private $_layout;
public function __construct()
{
$this->_layout = self::$default_layout;
}
public function setLayout($layout)
{
$this->_layout = $layout;
}
public function render($template = null)
{
if ($this->_layout != null)
{
$renderer = Kostache_Layout::factory($this->_layout);
$this->template_init();
}
else
{
$renderer = Kostache::factory();
}
return $renderer->render($this, $template);
}
}