Include User Custom Functions Best Practice in Symfony2 - php

I am a newbie in Symfony2 and I can't understand where I should make includes with my custom cross-projects functions (e.g. array_merge_overwrite, array_last, etc.)? I use both types of apps: web (MVC) and console (extends ContainerAwareCommand).
Or there is another "right way" for this?

Create a service and put your common functionality in it. For example, you can name it ArrayService and register it in the container as array.service. You can then access this service from controllers via
$this->get('array.service');
and from commands via
$this->getContainer()->get('array.service');
So, your code will look something like this:
$element = $this->get('array.service')->last($array); // or ->arrayLast($array)
If you need the same functionality across several projects, make a bundle with that service and add it to the deps file of each project. Then it will be installed when you run the bin/vendors install script.

You can convert your functions to static methods of some class to make them autoloadable. Or... well... Place them where you want and require() from where you need them every time.

Related

Where should I put a custom class file in zend framework 3?

I have created a set of class files that helps to create the route configuration array. I, however, do not know where to put the source files. I browsed around a little and found some answers that suggested I put up the code in packagist and install it via composer, but my classes are relatively small. So, I wanted to ask if there is another way to do it. The files must be accessible in all the modules.
Your application code is organised into modules and lives in module/<Modulename>/src/. If the code is something you might want to reuse in other applications, then it might make more sense to have it as a standalone library that you install via. Composer.
You're code is accessibly through all the application if it is configured in the composer.json's autoload section.
Code structure in ZF is quite simple. You have your modules and in every module you have an src directory. This src directory should have its own namespace. And there you can place your custom route configurator under this namespace, like 'ModuleName\RouteConfigurator'.
But if you use this logic through multiple modules, I suggest you to create a separate module for it. In this case, after a long road, you may consider creating a separate composer package from it. But it's not necessary.
If you're not familiar with defining modules, please read the zend-modulemanager's documentation (https://docs.zendframework.com/zend-modulemanager/intro/)

Common lib on symfony2

Before using Symfony2, I used to have a common lib with a lot of simple but useful functions (for instance, a function which takes "Azè_rtï" in argument and returns "aze-rti").
So, well, I created a Bundle: CommonLibsBundle.
But.. I have only one or two php files. It does not make sense to me to use a controller / view / model in this kind of situation.
What should I do? May I erase all folders in my new bundle (Controller, DependencyInjection, Resources, Tests... + CommonLibsBundle.php) and just put my lib.php in it?
Many thanks,
Bliss
Unless you need to tap into the Symfony framework itself - for configuration or to define services, it doesn't need to be a bundle - it's just a library. Give it a reasonable namespace, and call as required as you would any other component or library.
Even if you wanted to add Symfony-specific services that you could call, there is something to be said to still have an external simple library - usable anywhere, which then is wrapped by a very thin bundle which would only add the Symfony-specific (or Laravel, or ZF, or whatever) services and configuration as required.

Advice needed for Laravel 4 project structure

I'm quite new to Laravel 4 (Laravel is my first PHP MVC framework) and need a little help structuring my project.
1/ I need to include the the json-rpc classes found here jsonrpcphp.org in my project and am not quite sure the best place to put these.
2/ Where is the best place to do my initial setup/connection to my data source? Should this be inside of my controller or should this be placed elsewhere?
require_once 'jsonRPCClient.php';
$connection = new jsonRPCClient('http://user:password#127.0.0.1');
I think and pretty sure the best way to do it is to create a Service
Put anywhere securely and can be easily accessed via composer. Look into how add autoloading to composer http://getcomposer.org/doc/01-basic-usage.md#autoloading
as I said, create a service. look into Laravel IoC Container http://laravel.com/docs/ioc and How to define Service Provider
see example: DatabaseServiceProvider
Related: optional for nice syntax create a Facade for it.

creating zf custom commands, like zf create form

When I use the zend CLI to create a form, like this, a form gets generated and gets added to the forms folder.
zf create form MyForm
I have a custom class that I'd like to create a custom command for it in the same way and would like to look at the zf create form command for guidance. I suspected it to be in Zend_Framework/bin where there's a shell, php, bat files, but I couldn't find any code there related to the form class creation, maybe i missed?
Anyway, is there a better to accomplish the same thing? like an Zend API that lets me create some of these commands myself? If not my only option would be to see how something like zf create form was created.
When you run zf, it invokes zf.php in the framework's bin folder. That in turn creates an instance of Zend_Tool_Framework_Client_Console, which jumps through some non-obvious hoops to load up all of the available actions and parse your command. Part of this process involves looking in the /Zend/Tool/Project/Provider/ directory, which houses the classes responsible for providing many of the project-oriented actions (like creating forms).
I haven't ever tried this myself, but there is (apparently somewhat outdated) documentation on creating your own providers, so hopefully following that will get you pointed in the right direction.
The ZendCast Integrating Zend_Tool Into Your Application is an excellent introduction to the whole business of creating Zend_Tool providers.
You need to create a provider. Look at this nice tutorial : http://weierophinney.net/matthew/archives/242-Creating-Zend_Tool-Providers.html

Is .zfproject.xml a must in a Zend Framework project?

Is .zfproject.xml a must in a Zend Framework project?
What does it do?
Is it's location absolute?
When using Zend_Tool to manage your Zend Framework project, .zfproject.xml will contain your application structure state. This is required by Zend_Tool (and only by it) to be able to work, e.g. add code to certain parts., generate things, etc.
Quoting ZF Manual on Zend_Tool_Project:
So, for example, if in one command you created a controller, and in the next command you wish to create an action within that controller, Zend_Tool_Project is gonna have to know about the controller file you created so that you can (in the next action), be able to append that action to it.
I am not sure if Zend_Tool can be configured to use a different path to .zfproject.xml. My suggestion would be to leave it untouched. It's a hidden file anyway.
Just to add, the zfproject.xml is not needed if you don't use Zend_Tool.
So it's not a must. Personally, I manage all my zf projects more or less without a command line and it works fine for me.
Basically if your going to use Zend_Tool, stick with it. Zend Tool doesn't like it when you create MVC manually. Its just another layer of abstraction that you can probably live without.
I am using zend framework 1.10. Whenever I create a action using zf tool it re-indents the code in the controller file and removes some function closing brackets. It is kind of buggy, so will not be using it from now on.

Categories