PhalconPHP and phpunit - php

I've been following the docs to set up phpunit with phalcon. I have the example working.
However, I now want to actually put it to use and test my own classes, to do this I understand I need to register the directories in the loader, but instead of repeating the directories that are already defined in the config, I'm wanting to include the config in the php unit test helper class (from a `phalcon project' command). This sounds simple but is anything but.
Do I have to add additional code to access the $config?
Am I right in assuming that the docs are missing a large amount of code regarding testing your own classes, or, should it work straight out the box after following the docs?

I have tried to implement this myself the basic idea is below for an implementation example head to: https://github.com/SavvySoftWorksLLC/phalcon_3_2_devtools_sample_project/tree/pks/setup_phpunit
If you externalize your loader like
<?php
$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerDirs(
[
$config->application->controllersDir,
$config->application->modelsDir
]
)->register();
And then call it in your entrypoint like this:
include ROOT_PATH . "/../app/config/loader.php";
You will be able to reuse the same loader in your test helper provided you initialize a fresh DI container like the docs mention.
To give the big picture I pushed up a out of the box app create by devtools for you to look at.
Loader: https://github.com/SavvySoftWorksLLC/phalcon_3_2_devtools_sample_project/blob/master/app/config/loader.php
EntryPoint:
https://github.com/SavvySoftWorksLLC/phalcon_3_2_devtools_sample_project/tree/master/public

Related

Where do I save Zend_Form files?

I'm trying to learn Zend Framework! I'm quite interested in it but I can't find a tutorial which says where it's suppoused to be a Zend_Form class stored! Maybe it's something quite straightforward but I can't get it yet...
I've seen tutorials about this:
<?php
class Form_Example extends Zend_Form
{
public function init()
{
// Great code here
}
}
But none of them said where this code goes????? In a file in which folder in the directory tree?? I've read and I understand and I've done a little example with modules, controllers, actions, layouts and I know the importance about name conventions and the folder structure. So where does this form class must go and how can I call it from a view??
Thanks a lot, I know this must be easy for someone who already knows how to work well with Zend Framework =)
The best way to do this is to let ZF do it for you. ZF ships with a command line interface for both windows and *nix.
At the command line you can type zf create form Example, ZF will then create an empty form named Example.php at it's default application level location.
Typically this will be at application/forms/Example.php and the classname will be Application_Form_Example.
If you need to have a form constructed in a module the command would be similar:
zf create form Example -m admin where -m indicates you want the file created in a module and admin is name of the module.
Forms are one of the predefined resources in Zend Framework and as such have a default location. There are several other resources that are predefined and have defaults.
The Module Resource Autoloader
Zend Framework ships with a concrete implementation of
Zend_Loader_Autoloader_Resource that contains resource type mappings
that cover the default recommended directory structure for Zend
Framework MVC applications. This loader,
Zend_Application_Module_Autoloader, comes with the following mappings:
forms/ => Form
models/ => Model
models/DbTable/ => Model_DbTable
models/mappers/ => Model_Mapper
plugins/ => Plugin
services/ => Service views/
helpers => View_Helper
filters => View_Filter
As an example, if you have a module with the prefix of "Blog_", and attempted to instantiate the class
"Blog_Form_Entry", it would look in the resource directory's "forms/"
subdirectory for a file named "Entry.php". When using module
bootstraps with Zend_Application, an instance of
Zend_Application_Module_Autoloader will be created by default for each
discrete module, allowing you to autoload module resources.
I normally have all my forms in a forms folder, alongside the models, controllers, and views.
So, my file structure looks like:
application ->
configs
layouts
plugins
controllers
models
views
forms ->
form1.php
form2.php
Using them in your application isn't quite so simple. You must instantiate the form class in your controller, then pass the form to your view. So in your controller you want something like:
$form1 = new Application_Form_Form1($options);
$request = $this->getRequest();
if($request->isPost()) {
if($form1->isValid($post)) {
// form is valid, do form processing here
}
}
$this->view->form1 = $form1;
Then inside of your view file, you place the form:
<html>
<body>
<div id="body">
<?php echo $this->form1; ?>
</div>
</body>
</html>
At the heart of your question are the issues of:
autoloading
how the ZF autoloader works in general, and
how the ZF autoloader is configured by default in a standard ZF app
which are actually three distinct, though clearly-related, issues.
Assuming that you have the default ZF installation in which the appnamespace is set to "Application", then name your form class Application_Form_Example and store it in the file application/forms/Example.php.
Then you can instantiate (in a controller, for example) using:
$form = new Application_Form_Example().
Make sure that you have resources.modules[] = in application/configs/application.ini.
For additional discussion about autoloading, see https://stackoverflow.com/a/10933376/131824

How to get the server path to the web directory in Symfony2 from inside the controller?

The question is as follows:
How can I get the server path to the web directory in Symfony2 from inside the controller (or from anywhere else for that reason)
What I've already found (also, by searching here):
This is advised in the cookbook article on Doctrine file handling
$path = __DIR__ . '/../../../../web';
Found by searching around, only usable from inside the controller (or service with kernel injected):
$path = $this->get('kernel')->getRootDir() . '/../web';
So, is there absolutely no way to get at least that 'web' part of the path? What if I, for example, decided to rename it or move or something?
Everything was easy in the first symfony, when I could get like everything I needed from anywhere in the code by calling the static sfConfig::get() method..
There's actually no direct way to get path to webdir in Symfony2 as the framework is completely independent of the webdir.
You can use getRootDir() on instance of kernel class, just as you write. If you consider renaming /web dir in future, you should make it configurable. For example AsseticBundle has such an option in its DI configuration (see here and here).
To access the root directory from outside the controller you can simply inject %kernel.root_dir% as an argument in your services configuration.
service_name:
class: Namespace\Bundle\etc
arguments: ['%kernel.root_dir%']
Then you can get the web root in the class constructor:
public function __construct($rootDir)
{
$this->webRoot = realpath($rootDir . '/../web');
}
You also can get it from any ContainerAware (f.i. Controller) class from the request service:
If you are using apache as a webserver (I suppose for other
webservers the solution would be similar) and are using
virtualhosting (your urls look like this - localhost/app.php then you can use:
$container->get('request')->server->get('DOCUMENT_ROOT');
// in controller:
$this->getRequest()->server->get('DOCUMENT_ROOT');
Else (your urls look like this - localhost/path/to/Symfony/web/app.php:
$container->get('request')->getBasePath();
// in controller:
$this->getRequest()->getBasePath();
You are on Symfony, think "Dependency Injection" ^^
In all my SF project, I do in parameters.yml:
web_dir: "%kernel.root_dir%/../web"
So I can safely use this parameter within controller:
$this->getParameter('web_dir');
My solution is to add this code to the app.php
define('WEB_DIRECTORY', __DIR__);
The problem is that in command line code that uses the constant will break. You can also add the constant to app/console file and the other environment front controllers
Another solution may be add an static method at AppKernel that returns DIR.'/../web/'
So you can access everywhere
UPDATE: Since 2.8 this no longer works because assetic is no longer included by default. Although if you're using assetic this will work.
You can use the variable %assetic.write_to%.
$this->getParameter('assetic.write_to');
Since your assets depend on this variable to be dumped to your web directory, it's safe to assume and use to locate your web folder.
http://symfony.com/doc/current/reference/configuration/assetic.html
For Symfony3
In your controller try
$request->server->get('DOCUMENT_ROOT').$request->getBasePath()
$host = $request->server->get('HTTP_HOST');
$base = (!empty($request->server->get('BASE'))) ? $request->server->get('BASE') : '';
$getBaseUrl = $host.$base;
Since Symfony 3.3,
You can use %kernel.project_dir%/web/ instead of %kernel.root_dir%/../web/

Zend Framework - Form class not autoloading

I'm getting
Fatal error: Class 'Form_Login' not found in /route/to/project/application/controllers/AuthController.php on line XX
when instantiating the class From_Login inside the controller.
I suppose the form is not being autoloaded by the bootstrap class.
In my bootstrap file I have this method
protected function _initAutoload(){
$modelLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH));
return $modelLoader;
}
supposed to autoload my resources.
I'm using the default project structure.
-application
--controllers
---Authcontroller.php
--forms
---Login.php
when I created the form with zf tool it automatically set the name as Application_Form_Login then I erased the Application_ part since I'm using "" namespace. I doesn't work either way.
I've also tried setting appnamespace="" in the application.ini file but nothing happened
After trying over and over different options I got tired because it didn't work so I erased the project folder and started from the beginning whit zend tool and ... voilĂ , it works!
In my opinion it was a problem with zend tool and/or the .zfproject.xml file since I was adding some resources manually and some others with the zf tool.
use Zend modular structure and change your class name 'Form_Login' to 'Default_Form_Login' .

How can I bootstrap Symfony into a standalone script?

(I hope 'bootstrap' is the correct term...)
I have a Symfony 1.4 project in which I'm using a PHP script that mostly contains Javascript (I'm including this script with a simple <script src="/js/myStuff.js"></script> tag). I need to use some Symfony classes, helper methods, and variables from within the script (specifically the sfConfig class, url_for() helper method, and the $sf_request variable.) I'm at a loss as to how to achieve this. I tried copying the code from one of the front controllers into the script, but that ended up outputting the contents of my application's layout file.
Thanks in advance!
You can do what you want by using something like this to create a symfony context:
require_once($_SERVER['DOCUMENT_ROOT'].'/../config/ProjectConfiguration.class.php');
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false);
$context = sfContext::createInstance($configuration);
To use url_for, you will also need to either load/include the Url helper, which can be done like:
sfContext::getInstance()->getConfiguration()->loadHelpers('Url');
I think there's a better approach though:
Serve this javascript file as symfony action if you need access to symfony - there's nothing that says you can only serve html through symfony.
Check out the block here entitled Javascript As An Action for an explanation ...
http://www.symfony-project.org/jobeet/1_2/Doctrine/en/18#chapter_18_user_feedback

How to use different bootstraping for different modules in zend framework

I have two modules, default and mojo.
After the initial bootstraping code which is the same for both of the modules, I want, for example, to use different layouts for each module (Or use different credentials check etc).
Where do I put this: IF(module=='mojo') do this ELSE do that
If you are using Zend_Application (in ZF1.8) then you should be able to use the module specific configuration options to provide this functionality with a as explained in the relevant section in the documentation.
This would require you to set the layout in the config so it looked something like
mojo.resources.layout.layout = "mojo"
anothermodule.resources.layout.layout = "anotherlayout"
The layout would then be set automatically by the bootstrap.
The other alternative is to use a front controller plug-in that implements the preDispatch() method to set the layout based on the module name.
hmm i havent tried this
http://www.nabble.com/Quick-Guide-How-to-use-different-Layouts-for-each-module-to23443422.html#a24002073
the way i did that now was thru a front controller plugin
something like
switch ($request->getModuleName()) {
case "":
// set layout ...
}
I've looked into the subject a couple of days ago, trying to get it to work on bootstrap config alone. The big problem is that all the bootstrap files are loaded, so it gives some weird results in which layout is used.
My conclusion was that you can have the config in place, but you need to work with FrontController plugins or ActionController helpers. If you want to use config set in the application.ini and you want to load the config trough the bootstrap, helpers is the only way to go. From the helper, you can then load the ActionController and on that execute the getInvokeArgs to load the bootstrap. A lot of hastle... :)
Anyway, I've done a small implementation as an example in a blog post: http://blog.keppens.biz/2009/06/create-modular-application-with-zend.html
Goodluck,
Jeroen

Categories