Zend General Functionality - php

I am learning how to use the Zend Framework. I come from a codeigniter background.
What I want to do is define a function somewhere that performs a very simple yet useful function. I am predominantly going to use the function within view scripts. I don;t really want to make a whole class for such a simple thing, so my question is, is there anywhere were can I put a file containg all of my general functions and how do I go about using it?
Thanks
John

What you are looking for are view helpers.
A view helper however is a function in a helper class. Therefore only one view helper can be put in a single class.
If you are using the project setup as used in the quick start tutorial or as generated by Zend_Tool, your view helpers should be put in the application/views/helpers directory.
Declaring a view helper is pretty simple, and is explained in great detail on this page of the zend framework documentation (i must say it's a bit hidden in the docs):
http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.custom
Some background information on view helpers as well as some standard included ones can be found on this page: http://framework.zend.com/manual/en/zend.view.helpers.html
Hope this helped you in the right direction.

If you realy whant to use a function you can make a library class with a static method , make a folder like this Application/Library/MyLib , then at bootstrap register MyLib namespace like this
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('MyLib'); , then inside MyLib folder you can make a filename MyClass , with a class name MyLib_MyClass , then inside you're view you can call MyLib_MyClass::staticMethod().
Tough i suggest you make a view helper for this . You don't realy use functions in ZF like you where used to in CI ( i was in you're exact situation a few months ago ) , ZF is all about OOP .

Related

CakePHP 3 include custom lib

In CakePHP 2.x I had static classes inside:
\app\Lib\Util\MyStaticClass1.php
\app\Lib\Util\MyStaticClass2.php
Then in app\Config\bootstrap.php file I added these lines.
App::uses('MyStaticClass1', 'Lib/Util');
App::uses('MyStaticClass2', 'Lib/Util');
So I didn't include these classes in every controller, component, helper or ctp file.
How can I do this in CakePHP 3 ? I tried this:
I copied these files to
\src\Util\MyStaticClass1.php
\src\Util\MyStaticClass2.php
Then inside every controller, component, helper and ctp file I added this:
use App\Util\MyStaticClass1;
use App\Util\MyStaticClass2;
This works. But is there an easier way to include these files ?
But is there an easier way to include these files ?
I don't think so that's how the namespaces work. You could wrap the static method class in traits. See the EventManagerTrait of the core for example.
I had static classes (...) inside every controller, component, helper
This is bad practice and introduces tight coupling. I think your application architecture needs a serious overhaul if you used them everywhere extensively.

Custom load class

Hi I would like to know if it is possible to make custom load class for codeigniter. For my theme switcher system I am making.
I would like to be able to do some thing like this. I am stuck on this issue?
$this->load->controller('folder/file');
$this->load->controller('folder/folder/file');
"controller" is a custom function.
So can do this $data['name] = $this->load->controller('folder/file');
It just would make my life a lot easy if I could do it that way.
I am trying to make my own version of an min hmvc system. I know they always ask why do you need it. Always have to keep explaining it. Lots of people are after this way.
CodeIgniter manage it's class, helper, view, model, database.. loading in
system/core/Loader.php
It's why you can use
$this->load->view();
$this->load->database();
$this->load->model();
$this->load->database();
So, you can extends CI_Loader in
application/core/MY_Loader.php
add controller function and whatever you want
I found a demo in this blog post:
http://www.techsirius.com/2013/01/load-controller-within-another.html
Edit:
If you want to apply to other folder like subfolder, you can add some logic to $file_name
$file_path = APPPATH.'controllers/'.$file_name.'.php';
I think it's not possible to load a controller from an other controller.
You should use Helpers instead :)
http://www.codeigniter.fr/user_guide/general/helpers.html

How to load a class in Zend Framework

I got a PHP library (PHP Markdown) in my library fold in an standard Zend Framework application. What is the best way to load the file and all it's classes to use in my models and controllers.
Structure:
library/phpMarkdown/markdown.php
Note:
PHP Markdown has a really ugly structure: It's only real "API" is a simple function, not a class. So the elegant was do not work for this exact case, but regarding the question the genearl solution the correctly named files/class is also "the right answer.
Edit
So much good input here, really not sure which answer I should accept! Thanks to you all!
The autoloader
Just instantiate the class and the autoloader should find it. If it doesn't you need to add the namespace and path.
If you have a class in the following tree (for exemple) : library/My/Tool.php
You will need to add this to your application.ini :
autoloaderNamespaces[] = "My_"
And then in your code you just call :
$tool = new My_Tool();
Edit :
in the file Tool.php you must follow the Zend Naming Conventions and have something like this :
<?php
class My_Tool {
}
For more informations see this : Zend Naming conventions
To keep it simple and just add that one file, you could put something like this in your Bootstrap.php:
protected function _initLoad(){
Zend_Loader::loadFile('markdown.php', '/../library');
}
I just copied markdown.php into the application library and put this little function in the bootstrap. You could also use Zend_Loader::loadClass(); if you want.

ZF: Where to put custom function

I have an enCrypt and deCrypt class which I want to use in my whole Zend Framework project without having to declare it on every need, but just once. Where should this be done? Thank you for any help...
Zend Framework is quite flexible in implementing things.
You could create an instance in a bootstrap file and save it to the registry; whenever you need to call the class just get it from registry?
You could have every controller extend Zend_Controller_Action and put the two functions into this class (only ideal if you're calling the classes from the controller).
Or, what I did, was make my functions static, register my own library (in your application config.ini file, enter the line: autoloaderNamespaces[] = "MyPrefix_", then create a folder in the library folder called MyPrefix) and drop my class in there. When I need it I call $encryptedString = MyPrefix_Crypt::encrypt($string); and $string = MyPrefix_Crypt::decrypt($encryptedString);
Hope this helps :)

Loading custom classes in CodeIgniter?

Just starting to use CodeIgniter, and I'd like to import some of my old classes for use in a new project. However, I don't want to modify them too much to fit into the CI way of doing things, and I'd like to be able to continue to use NetBeans' autocomplete functionality, which doesn't work too well with CI.
So, what is the best way to load custom classes & class files into CodeIgniter without having to use the library/model loading mechanisms?
I apologise if this is something I should be able to find quickly, but I can't seem to find what I'm after. Everything I see is just telling me how to go through CI.
To do it codeigniter way, place your custom classes in libraries folder of codeigniter. And then use it by adding that class as library in your controller like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Someclass {
public function some_function()
{
}
}
/* End of file Someclass.php */
using in controller:
$this->load->library('someclass');
checkout complete article at http://www.codeigniter.com/user_guide/general/creating_libraries.html
Libraries are easy to write but they have a few restrictions. Constructors can only take an array as a parameter and it's assumed that only one class will exist per file.
You can include any of your own classes to work with them however you want, as this is only PHP ofc :)
include APPPATH . 'classes/foo.php';
$foo = new Foo;
Or set up an __autoload() function in your config.php (best place for it to be) and you can have access to your classes without having to include them.
I'd say you at least write a wrapper class that could require the classes and instantiate the objects and make them accessible. Then you could probably autoload such library and use it as needed.
I would recommend that you at least tried to have them fit in the CI way, as moving forward this will make you life much more easy. I've been in kind of the same position and learned just this along the way.
require_once(PHYSICAL_BASE_URL . 'system/application/controllers/abc.php');
$report= new abc();
Next use the function detail in abc contoller:
$mark=$report->detail($user);
If you're just starting to use CodeIgniter, maybe you ought to check Kohana (http://kohanaframework.org/). It is very similar to CodeIgniter in many ways but it loads classes in the normal way (using new ClassName()) so Netbeans' autocompletion features should works normally.

Categories