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.
Related
I'm using silex for one of my projects and usually when i want render a twig template I use this syntax :
$app['twig']->render('page.twig');
in this project I want to use the trait to have this syntax :
$app->render('page.twig');
to do that I modified the Application class in Silex\Application and added :
use TwigTrait;
so my question is : if its ok to do that and modify this class or if not, is there another way to do that.
thanks in advance and I apologize for my bad English.
Instead of modifying the class, just extend it. The Silex test suite has an example. Then just use it like you would the normal application class.
$app = new MyTwigApp();
I am using ZF2 and I want to use Zend_Config_Xml. looking at the library, the directory structure looks like so...
Zend/Config/Config.php
Then there is
Zend/Config/Reader/Xml.php
But that does not contain the class either. Where is the Zend_Config_Xml class ???
There is no Zend_Config_Xml class in ZF2, Zend_Config_Reader_Xml is the equivalent. (Zend_Config_Xml was a ZF1 thing.)
The docs has usage examples: http://framework.zend.com/manual/2.2/en/modules/zend.config.reader.html#zend-config-reader-xml
I'm making now things with php + Symfony2 framework, and I have the following code:
require_once("one_file.php");
require_once("another_file.php");
... and so on.
The problem is, how to "Symfonyze" these uncomfortable require sentences, and after, how to include these files in the Symfony2 package?
We've thought about two possibilities:
Include the file at /vendors directory of symfony, or
Include each class as a service.
If these classes reside inside bundle then you could use as below:
Suppose your bundle name is AcmeDemoBundle. Place this file inside Acme/DemoBundle/Model/
//one_file.php
namespace Acme/DemoBundle/Model;
class one_file {
...........
}
To use this file inside your controller or any other file:
Here for Acme/DemoBundle/Controller/UserController.php
namespace Acme/DemoBundle/Controller
use Acme/DemoBundle/Model/one_file
class UserController {
public $one_file=new one_file();
}
In php 5.3 onwards, namespaces has been introduced. You should probably look at namespaces and its uses in php documentation
You can follow the PSR-0 standard to let the autoloader handle this. See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md and http://getcomposer.org/doc/04-schema.md#psr-0 .
Or you could keep your files as is, and tell composer to require them each time : http://getcomposer.org/doc/04-schema.md#files
You have to make a folder in your acme folder like lib puts these files in lib folder then use this statement
use Acme\DemoBundle\lib\Abc; // its your class name
You want to follow these other answers, especially the approved one, but if you are using a third party library with tons of PHP files, you can do require_once(__DIR__.'/path/to/file.php') as a quick fix.
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 .
I came across the __autoload function in PHP and would like to use it with my MVC folder structure. The function itsself is pretty easy, but how do I achieve a dynamic folder scanning after some kind of naming, please see example:
-application
--controller
--models
---entities
----house
---factories
----houseFactory
--views
-library
-public
As you maybe recognise its very close based on zend framework or other frameworks -as I come from those, however I would like to develop a website without framework and just started to write bootsrap file.
Maybe sombody could help me with the autoload in this - i think - advanced usage.
My Classnames will be like
Model_Entities_House
or
Model_Factory_HouseFactory
witch can be applied to the folder structure.
What I do mostly is use the SPL autoload function, which will help you to accomplish this quite easily. It should be something like this:
spl_autoload_register("MyClass::Autoloader");
Then, you can do something like this
class MyClass
{
public static function Autoloader($className)
{
//parse $className and decide where to load from...
}
}
If you´re using a naming convention, then you should be available to load the required file by just using the name.