using TwigTrait in silex - php

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();

Related

How to use facades and libs directly from views

I have my Laravel app setup in a pretty standard way and a bunch of external libs installed via Composer.
Laravel does all the boostrapping by pulling the .env variables and establishing database connections. At the top of each controller I have, for example:
namespace App\Http\Controllers\Users;
use GuzzleHttp;
use MongoClient;
class UsersController extends Controller {
public function index() {
return "foobar";
}
}
I would like to know how to require existing facades and DB connections in my views. I do realize that this is a bad practice but I would really like to understand how to use for example MongoClient directly from within the view.
Thanks!
since Laravel use the MVC (Model Vue Controller) pattern, i can confirm that it's really a bad practice and i really don't recomand to do it.
what do you want to acheive can be seen as a dependency injection (But it's really not)
there is 2 ways to proceed :
plain php behave : you declare a php block in the top of your file.blade.php
and there further in the your file.blade.php you make for example
<?php
MongoClient::someMethod(...)
?>
second way you inject it as a dependency injection with the attribute
#inject('mongoExampleName', 'App\Services\MongoService')
But then you have to create a service container for it and bind it with the service provider.
In the end i really dont recomand this, it was just to show the possibilities.
Good luck

issues trying to use Zend_Config_Xml where the heck is it?

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

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.

Zend General Functionality

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 .

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