ZF 1.12 custom view helper - failed opening - php

I'm working on restructuring my Zend 1.12 project. I have a couple of view helpers:
OutputComplexForm.php
OutputDistributorsList.php
I put them in /application/views/helpers
Class names are
Zend_View_Helper_OutputComplexForm
Zend_View_Helper_OutputDistributorsList
As I understand if you have Zend_View_Helper prefix you don't need to add any configs to application.ini
Now, when I try to load any page (even those which don't use helpers) I receive error:
Message: Zend_Session::start() - /otms/vendor/zendframework/zendframework1/library/Zend/Loader.php(Line:134): Error #2 include_once(): Failed opening 'Zend/View/Helper/OutputComplexForm.php' for inclusion (include_path='/otms/application/../library:/otms/application/../library/phpseclib0.3.1:/otms/application/../library/Amazon:/otms/application/../library/USPS:/otms/application/../library/Composer:/otms/library:/otms/vendor/phpseclib/phpseclib/phpseclib:/otms/vendor/zendframework/zendframework1/library:.:/usr/share/php:/usr/share/pear')
Did I miss something?
UPD
I've found out that error occurred after calling function
$startedCleanly = session_start(); //line 482
in file /Zend/Session.php. After this call property Zend_Session_Exception::$sessionStartError contains described error message. I still don't see connection between starting session and initialising view helper.

For application-specific classes that you write - stuff that appears inside ./application/* - should typically not be in the Zend_ pseudo-namespace. Rather, they should be in the appnamespace, as configured in ./application/config/application.ini.
The default namespace is 'Application_', so a view-helper called MyHelper would typically be stored in the file ./application/views/helpers/MyHelper.php:
class Application_View_Helper_MyHelper extends Zend_View_Helper_Abstract
{
public function myHelper()
{
// do your stuff here
}
}
Note that the class name is upper-camel-case MyHelper and the method is lower-camelcase myHelper().
In your view, you can invoke your view-helper with:
<?php
$output = $this->myHelper();
// Do something with $output
With these conventions on namespace, class name, and file location/name, and invocation syntax, the View's plugin loader should be able find, load, and execute your view-helper method.

Related

Namespace conflict when using 3rd party api in class

There is something weird going on. I'm a novice with respect to php programming.
I'm trying to use FirePHP but my question is actually not related to that tool directly.
The function fb you'll see below is part of FirePHP. My entry point is Main.php. The first fb call is executed without any problems but the second call (see ExperController.php) ,which gets triggered when $ec->exper(); in Main.php is called, causes a fatal error:
Fatal error: Uncaught Error: Class 'App\Controllers\FirePHP' not found in path-to-wp-directory\wp-content\mu-plugins\typerocket\app\Controllers\ExperController.php on line 12
Why is the runtime engine looking for the class FirePHP under that namespace (App\Controllers)? And why wasn't this an issue during the first fb call? There isn't any namespace defined in the FirePHP files at all. And my last but crucial question, how can I fix this without having to touch 3rd party files? This answer is not a solution for my case since I'm not referencing / calling the mentioned class in my code!
Note: FirePHP is included via autoloader (provided by composer).
Main.php:
<?php
fb('Hello World!', FirePHP::INFO);
$req = new \TypeRocket\Http\Request();
$res = new \TypeRocket\Http\Response();
$ec = new \App\Controllers\ExperController($req, $res);
$ec->exper();
ExperController.php:
<?php
namespace App\Controllers;
use TypeRocket\Controllers\Controller;
class ExperController extends Controller
{
public function exper() {
fb('Hello World!', FirePHP::INFO);
}
}
do you use any Dependency manager like composer??
when you call it from main somehow php can see where is FirePHP class is and everything works fine but when you try to access it inside a class in another folder you must define where the class is inside your controller , like how you defined where is controller class that you extend :
use TypeRocket\Controllers\Controller;
so add FirePHP class to your controller and everything will be ok then.
use Path\To\Class\FirePHP;
also if you did all and still not working you may try dump autoload;
composer dump-autoload

PrestaShop module classes not found (namespaces)

This is my PrestaShop module file structure:
-mymodule/
--src/
--mymodule.php
---Presta/
---Webhooks.php
----Controller/
-----MyPrestaController.php
mymodule.php cannot find Webhooks.php class, I've tried use in mymodule.php, but still it provides errors:
ClassNotFoundException in mymodule.php line 55:
Attempted to load class "Webhooks" from namespace "src\Presta".
Did you forget a "use" statement for another namespace?
When I try to use autoload/include/require in mymodule.php it throws fatal errors, because autoload initialize stuff(from my module vendor) that shouldn't be initialized in mymodule.php. GuzzleClient gets crazy while browsing website:
Catchable Fatal Error: Argument 3 passed to
GuzzleHttp\Client::request() must be of the type array, string given,
called in /usr/local/ampps/www/presta/modules/mymodule/vendor/guzzlehttp/guzzle/src/Client.php on line 89 and defined
I don't want to put all hook logic in mymodule.php and I have other classes that I need to implement in webhook methods. Is there any way to use other classes in main module file(mymodule.php)? Am I missing something?
You need to call your class with full path or declare a use on top of your module file. I don't know exactly what namespace Webhooks is under but something like this:
public function hookActionAuthentication($params)
{
\src\Presta\Webhooks::myStaticWebhooksMethod($params);
}
or
use src\Presta\Webhooks; // before module class declaration
public function hookActionAuthentication($params)
{
Webhooks::myStaticWebhooksMethod($params);
}

Zend Framework cannot get helper, even though it is list in existing helpers

I've previously created an action helper Website_Layout_ActionHelper which extends Zend_Controller_Action_Helper_Abstract and has a getName method which returns "layoutSelector".
This helper has been working fine for years along with several other helpers that have been put together, although none of them call on any of the others.
I'm now trying to create a new action helper which the layout helper needs to call on.
The new action helper is Website_UserInfo_ActionHelper - it is set up in practically the same way as the other helpers (file paths, etc), and has a getName method returning userInfo.
However, when I try to access the new helper from my layout helper, I get the following exceptions:
Fatal error: Uncaught exception 'Zend_Loader_PluginLoader_Exception' with message 'Plugin by name 'UserInfo' in /usr/share/php/Zend/Controller/Plugin/Broker.php on line 336
Zend_Loader_PluginLoader_Exception: Plugin by name 'UserInfo' was not found in the registry; used paths: Zend_Controller_Action_Helper_: Zend/Controller/Action/Helper/ in /usr/share/php/Zend/Loader/PluginLoader.php on line 424
Zend_Controller_Action_Exception: Action Helper by name UserInfo not found in /usr/share/php/Zend/Controller/Action/HelperBroker.php on line 369
However, when I dump the array from Zend_Controller_Action_HelperBroker::getExistingHelpers() instead of trying to call Zend_Controller_Action_HelperBroker::getHelper('userInfo') the userInfo helper is there...
The fact that the error messages are capitalising the UserInfo name is probably a clue, but I don't understand why I'm getting this behaviour when all the other helpers are happily working. Is it because I'm trying to access the helper from inside another helper? Because in my reading, this should be possible.
So, the question: why do I get exceptions when trying get a helper from the broker when the helper is actually loaded?

codeigniter include common file in view

Hi all I have a site developed in codeigniter and I wanto to store into a file called common.php some javascript/PHP function that I use in many pages.
I have tried in this mode:
require(base_url().'application/libraries/common.php'); //I have tried also include
This return me this error:
A PHP Error was encountered
Severity: Warning
Message: require() [function.require]: http:// wrapper is disabled in the server configuration by allow_url_include=0
I'm going to my php.ini and I turn On allow_url_include, restart apache and when I try to load the page return me now this error:
A PHP Error was encountered
Severity: Warning
Message: require() [function.require]: http:// wrapper is disabled in the server configuration by allow_url_include=0
Filename: backend/hotel_view.php
Line Number: 6
A PHP Error was encountered
Severity: Warning
Message: require(http://demo.webanddesign.it/public/klikkahotel.com/application/libraries/common.php) [function.require]: failed to open stream: no suitable wrapper could be found
Filename: backend/hotel_view.php
Line Number: 6
Fatal error: require() [function.require]: Failed opening required 'http://demo.webanddesign.it/public/klikkahotel.com/application/libraries/common.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/public/klikkahotel.com/application/views/backend/hotel_view.php on line 6
What can I do to include a simple file into my pages?
Pull it into whichever views you want using $this->load->view('common'); You can include other views from either the controller or the view.
Example 1
your_controller.php
public function index() {
$this->load->view('homepage');
}
views/homepage.php
<?php
$this->load->view('common');
?>
<body>
<!-- html -->
</body>
Example 2
your_controller.php
public function index() {
$this->load->view('common');
$this->load->view('homepage');
}
You should use APPPATH or BASEPATH or just type the full path to the file.
For security, require_once should be passed a local file, not a URL. I wouldn't really suggest using require_once() in CodeIgniter. It might be better to use:
$this -> load -> view('common_file');
How To Make Global Functions In CodeIgnitor
To create global functions, define them in CodeIgnitor Helper files and auto load them. Here's how:
Create Your Helper
To create [helpers][2], create a .php file in the application/helpers/ folder and save your functions there.
Note: It is good practice to use the following format, to avoid function name collisions:
if ( ! function_exists('my_function_name'))
{
function my_function_name($arg)
{
/* Your code here */
}
}
Making Them Global
If you use these functions all the time (Global), auto-load them.
Open the file: /config/autoload.php
Find the section $autoload['helper'] = array();
Add the name of your helper file (excluding the.php extension).
For instance, if you created a helper file called myhelper.php, It should look something like this:
$autoload['helper'] = array('myhelper');
Using Your Global Functions
Now your functions will be available throughout the site. Just call them wholesale:
my_sample_function('argument');
base_url() refers to the web path like http://localhost/myproject/. You cannot include a remote file, actually you should not. It's a security risk. See Can't include file on remote server
Building a custom library is a good choice and if you are using it a lot in your website, you can include it in application/config/autoload.php under the section $autoload['libraries']. It will autoload every time you reload the application/website based on codeigniter. Example: $autoload['libraries'] = array('common'); if your library is called common.php and is located in application/libraries/
DO NOT put functions into a viewer, that's why libraries and helpers exists. A viewer should contain only what a user should see. Example: a view is some form of visualisation of the model.
You should use APPPATH or BASEPATH or just type the full path to the file. require_once(APPPATH.'libraries/common.php');
You Can include anyware using
<?php $this->load->view('file'); ?>
For solve in more efficient way this problem I have done so:
You create a new helper (in application/helpers) with name (es. common_helpers.php, the underscore is important). In this file, you put all the functions for example build pieces of html in common.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function getHead(){
require_once(APPPATH."views/common/head.php");
}
function getScripts(){
require_once(APPPATH."views/common/scripts.php");
}
function getFooter(){
require_once(APPPATH."views/common/footer.php");
}
In your controller you call only one view in respect of MVC and call the functions from your custom helper.
class Hello extends CI_Controller {
public function index(){
$this->load->helper('common');
$this->load->view('index');
}
}
You can write php function in helper file
steps - create a helper file name common_helper inside application/helperfolder
- and create a function like
function getBoosters($id){
$ci=& get_instance();
$ci->load->database();
$sql = "select * from boosters where id ='".$id."' ";
$query = $ci->db->query($sql);
return $query->result();
}
This common function you can use where you want by loading this helper.
Suppose you want to use this method in FrontController simply load the helper by this line
$this->load->helper('common');
Now you can call the method. Add your js code in footer.php, all the functions are available on every page you can easily use them

Circular resource dependency error in Zend

I have a bootstrap class which I want to use to set CSS variables:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initView()
{
$this->bootstrap('view');
...
...
}
}
But trying to get the view resource fails at the bootstrap('view') stage. I get the error:
... Circular resource dependency detected' in C:\ZendFramework\library\Zend\Application\Bootstrap\BootstrapAbstract.php on line 662
...
Which is strange because this is the procedure that tutorials(and the zend documentation) use. What could be wrong?
Change the method to something like _initViewStuff() and all will be fine.
The reason is that the bootstrap sequence in Zend_Application_Bootstrap_BootstrapAbstract is as follows:
Your initial call to $app->bootstrap() in public/index.php runs through all _initXxx() methods (#see Zend_Application_Bootstrap_BootstrapAbstract::getClassResourceNames()) and calls $this->bootstrap('xxx') for each Xxx it finds. It will then do a similar thing for all the plugin resources defined by resources.* keys in application.ini (though yours never gets that far, as described below).
The call to bootstrap('view') internally stores a flag that he has started the process to bootstrap a resource called view.
He does a similar thing as (1), looking for a matching _initXxx() method. He find it and attempts to execute $this->_initView()
He notices the flag he set, indicating that he's gonna hit an infinite loop, so he bails out with circular dependency exception.
Typically, for each resource xxx, you bootstrap it using one (but not both, as you have discovered) of the following approaches:
Define an _initXxx() method.
Creating a plugin resource class named something like My_Application_Resource_Xxx (you inform that system that My_Application_Resource_ is a namespace prefix for plugin resources using pluginPaths.My_Application_Resource = /path/to/dir/containing/plugin in application/configs/application.ini)
You cant use this method name in your bootstrap class '_initView' because there is corresponding Zend_Application_Resource_View, just rename your bootstrap method name

Categories