I've the following super simple Zend Application:
Controller (IndexController.php):
public function indexAction()
{
$this->view->var_controller = "foo";
}
Layout (layout.phtml):
<?php
$var_layout;
echo $this->var_controller; ?> (1.)
When i set the breakpoint at echo output in layout script ("echo $this->var_controller") the breakpint is hit 2 times!
Does it mean the layuot script is executed 2 times or there is other explanation?
Related
I have the following code:
class IndexController extends pm_Controller_Action
{
public function indexAction()
{
shell_exec('php /usr/local/psa/admin/plib/modules/hello-world/script.php');
$file = file_get_contents( "/usr/local/psa/admin/plib/modules/hello-world/tune" );
var_dump($file);
var_dump(file_exists($file));
$this->view->message = $file ;
}
}
In /views/scripts/index/index.phtml
<?php echo $this->message; ?>
So it basically prints the content of "tune" file on a page, but I want it to execute the shell command (another php script actually) via shell_exec().
The problem is that shell_exec() does not work - script.php doesn't run.
I understand that I'm missing something very important here but can't figure out what.
p.s. sorry for such question - I'm new to php
I want to write a new Plugin in wordpress. My classes:
the plugin PHP-file:
<?php
/*
plugin-header (working)
*/
// Exit if accessed directly
defined('ABSPATH' || exit());
// Include classes
include('foo.php');
include('boo.php');
//add Init Hook
add_action('admin_menu','bohoo_admin');
function bohoo_admin() {
add_options_page('bohoo', 'someTitle', 'manage_options', __FILE__, 'createView');
}
function createView() {
$foo = new foo();
$boo = new boo();
return $foo->createFooDiv() . $boo->createBooDiv();
}
?>
My foo.php:
<?php
class foo {
public function __construct() {
}
public function createFooDiv() {
return '<div><h2>Hi</h2></div>';
}
}
?>
My boo.php:
<?php
class boo {
public function __construct() {
}
public function createBooDiv() {
return '<div> test </div>';
}
}
?>
Now what I basically tried is: The HTML-code should be in two different files and these should be concatenated and displayed (of course).
The way I understood integrating plugins in WP:
With the add_options_page-method you specify where your plugin is shown and what code is displayed. For the code you use the last argument (in this case the createView-method. This works so far if my createView() in my plugin PHP-file looks like this:
function createView() {
//include HTML-Code directly
?>
<h1>Hello World</h1>
<?php
}
?>
What happens when I include the boo.php and foo.php files instead of including the HTML-Code directly is nothing (So nothing is displayed and there is on error aswell). I am not sure what I am doing wrong, I also tried playing around with the HTML-Code in the return-statements of BOO and FOO, but that did not help either. What am I doing wrong? Or is it simply not possible to do it that way?
You need to print/echo your html content. Change createView to this:
function createView() {
$foo = new foo();
$boo = new boo();
echo $foo->createFooDiv() . $boo->createBooDiv();
}
That's how it's done in the add_options docs page.
And basically when you use php closing and opening tags:
?>
<h1>Some html</h1>
<?php
It's equivalent to printing the content in between:
echo '<h1>Some html</h1>';
I am developing an app where I need to log the proccess.
So I was loggin from main.php and now I need to log from another class (class_TestingLog.php)
Next code is how I am trying. Could you tell me what I am doing wrong?
Thank you in advance.
main.php
[...]
#Logging class
include_once("./classes/class_log.php");
$Class_Log = New Class_Log();
#TestingLog Class
include_once("./classes/class_testingLog.php");
$Class_TestingLog = New Class_TestingLog();
[...]
$Class_Log->FreeLog("Test log");
$Class_TestingLog->AnyFuncion();`
[...]
class_log.php
[...]
public function FreeLog($text)
{
// echo $text . "\n"; #mistake
$this->outputText .= text; #correct one
}
[...]
class_testingLog.php
private $Class_Log = '';
[...]
public function __construct()
{
#Requires
require_once("./classes/class_log.php");
$this->Class_Log = New Class_Log();
}
public function AnyFuncion()
{
var_dump($this); #From real file
$this->Class_Log->FreeLog("Test log from another classs");
}
[...]
Browser output
Test log
Browser output expected
Test log
Test log from another classs
===========================================================================
EDIT
I made an error copying FreeLog();
It stores the parameter string value into a private variable instead echo the variable.
You require_once statement inside __construct for Class_TestingLog is invalid and unnecessary. As both files are in the same directory it should be "class_log.php" not "./classes/class_log.php". There is no need for it anyway, as when you include them both in main.php it is loaded already. So try it without the require_once.
EDIT: As discussed in chat do it like this:
in main.php:
$Class_TestingLog = New Class_TestingLog($Class_Log);
in class_testingLog.php:
public function __construct($ClassLog)
{
$this->Class_Log = $ClassLog;
}
This will use the same instance inside the Class_TestingLog class as on the main.php.
KSmarty is a Kohana module meant to integrate Smarty with Kohana. I'm trying to migrate my current project (already using Smarty) to using Kohana.
I'm trying to get KSmarty set up, but I'm having difficulties getting the templates to work. This is the "hello world" example from KSmarty:
application/classes/Controller/Welcome.php
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Welcome extends Controller_Template
{
public $template = 'welcome';
public function action_index()
{
// Assign a value to the variable 'intro'
$this->template->intro = 'Hello world!';
// Create a nested view by loading a different template
$this->template->content = View::factory('content');
}
}
// End Welcome
application/views/welcome.tpl
<html>
<body>
<h1>{$intro}</h1>
<p>
{$content}
</p>
</body>
</html>
application/views/content.tpl
Yes, this works!
However, for me, the controller/view combo does not work as expected. Here are the variants of action_index() that I've tried:
public function action_index()
{
echo 'foo';
}
// Output: foo
public function action_index()
{
// Assign a value to the variable 'intro'
$this->template->intro = 'Hello world!';
// Create a nested view by loading a different template
$this->template->content = View::factory('content');
}
// No output
// No error in apache log, php log, or kohana log
public function action_index()
{
Ksmarty::instance()->assign(array(
'intro' => 'Hello world!',
'content' => APPPATH.'/views/content.tpl'
// Note: also changed {$content} in template to {include $content}
));
Ksmarty::instance()->display(APPPATH.'/views/welcome.tpl');
}
// Expected HTML output
I could simply use Ksmarty::instance() like this and get my website working, but this isn't how the Kohana Views system was designed, and it feels like a kludge, especially since the KSmarty example matches up with Kohana's use of Views.
I'm pulling my hair out trying to pin this one down, which is impressive considering the amount of hair-pulling Kohana gave me on the initial install. What am I doing wrong?
Oh, I have make two changes to KSmarty to reach this point:
All instances of Kohana::config('smarty') replaced with Kohana::$config->load('smarty'); as far as I can tell, this is a matter of a version change in Kohana.
Commented out $s->security = Kohana::$config->load('smarty')->security;; as far as I can tell, this is a matter of a version change in Smarty, and KSmarty is configured to FALSE anyway.
Adding echo $this->template; to the end of the view works. It's not in the Kohana nor the KSmarty documentation/examples, but it's close enough to satisfy me. If anyone else ever comes up with an answer that solves the problem without echo, I'll mark that answer as accepted, but until that time, I have a solution.
<?php defined('SYSPATH') or die('No direct script access');
class Controller_Welcome extends Controller_Template
{
public $template = 'welcome';
public function action_index()
{
// Assign a value to the variable 'intro'
$this->template->intro = 'Hello world!';
// Create a nested view by loading a different template
$this->template->content = View::factory('content');
// Output the view
echo $this->template;
}
}
// End Welcome
I wrote a service class called search_categorization_service.php. Now I am making a call to python scrpt in this class
class SearchCategorizationService
{
function searcher($query)
{
$tmp=passthru("python serverscript1.py $query");
ob_start();
$out=ob_get_contents();
echo print_r($out,true);
}
}
but i dont get any output on the browser. i tried returning it to a controller class and printing the output but it just wont work.any help wud be appreciated. is it an issue with cakephp? because the same application works fine in normal php.
Try moving ob_start() above $tmp=passthru("python serverscript1.py $query");. It appears nothing is being output after the output buffer is started.
<?php
class SearchCategorizationService
{
function searcher($query)
{
ob_start();
$tmp=passthru("python serverscript1.py $query");
$out=ob_get_contents();
echo print_r($out,true);
}
}
?>