Using ZF2 components without entire MVC process - php

How feasible is it to use the Zend Framework 2 components without using the MVC process? For example I love the forms / validation and ACL elements but am not sure if that is actually possible without the whole MVC system?
As a framework ZF2 is very slow (although I think its a very good system) so would like to encourage its use without the whole package. Thanks.

Yes. Zend Form component has a separate repository and it can be used in any application as a component with help of composer. (I'm assuming that you're using composer and your application also uses composer's autoloader) It requires only InputFilter and Stdlib components.
You can try easily. Open your command line:
$ cd /path/to/an-empty-folder
Create a composer.json file with the content below
{
"name": "Form Demo App",
"require": {
"php": ">=5.4",
"zendframework/zend-form": "2.3.*#dev"
}
}
and after type
$ composer update
Following dependencies will be installed automatically into the vendor directory and composer.lock will be created :
Loading composer repositories with package information
Installing dependencies (including require-dev)
- Installing zendframework/zend-stdlib (2.3.3)
Downloading: 100%
- Installing zendframework/zend-validator (2.3.3)
Downloading: 100%
- Installing zendframework/zend-filter (2.3.3)
Downloading: 100%
- Installing zendframework/zend-inputfilter (2.3.3)
Downloading: 100%
- Installing zendframework/zend-form (2.3.3)
Downloading: 100%
From performance viewpoint, ZF2 is not very slow. You just need to do couple of things on production environment to run your application much more more performant.

Related

Symfony 2.8 to 3.0 migration

I want to upgrade symfony 2.8 project to 3.0, i changed the version in composer.json to 3.0.* but after composer update is done, i have these error in console:
[RuntimeException]
An error occurred when executing the "'cache:clear --no-warmup'" command:
PHP Fatal error: Class 'Symfony\Component\HttpKernel\Kernel' not found in
/var/www/html/t/EcoPro/app/AppKernel.php on line 6
It sounds like your code can't find the Kernel class from Symfony. Since it should still be there, as you can see in the Symfony repository in the 3.0-branch I assume something went wrong during your update.
I suggest first running composer diagnose to see if composer itself is up to date and your composer.json is syntactically valid. As a next step you should verify the current version of your dependencies by running composer show. The output should look something like this:
$ composer show
doctrine/annotations v1.2.7 Docblock Annotations Parser
doctrine/cache v1.5.1 Caching library offering an object-oriented API for many cache backends
doctrine/collections v1.3.0 Collections Abstraction library
doctrine/common v2.5.1 Common Library for Doctrine projects
doctrine/dbal v2.5.2 Database Abstraction Layer
doctrine/doctrine-bundle 1.6.0 Symfony DoctrineBundle
doctrine/doctrine-cache-bundle 1.2.2 Symfony Bundle for Doctrine Cache
doctrine/inflector v1.1.0 Common String Manipulations with regard to casing and singular/plural rules.
doctrine/instantiator 1.0.5 A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer v1.0.1 Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
doctrine/orm v2.5.2 Object-Relational-Mapper for PHP
incenteev/composer-parameter-handler v2.1.2 Composer script handling your ignored parameter file
jdorn/sql-formatter v1.2.17 a PHP SQL highlighting library
monolog/monolog 1.17.2 Sends your logs to files, sockets, inboxes, databases and various web services
paragonie/random_compat 1.1.0 PHP 5.x polyfill for random_bytes() and random_int() from PHP 7
psr/log 1.0.0 Common interface for logging libraries
sensio/distribution-bundle v5.0.2 Base bundle for Symfony Distributions
sensio/framework-extra-bundle v3.0.11 This bundle provides a way to configure your controllers with annotations
sensio/generator-bundle v3.0.0 This bundle generates code for you
sensiolabs/security-checker v3.0.2 A security checker for your composer.lock
swiftmailer/swiftmailer v5.4.1 Swiftmailer, free feature-rich PHP mailer
symfony/monolog-bundle v2.8.2 Symfony MonologBundle
symfony/phpunit-bridge v2.8.0 Symfony PHPUnit Bridge
symfony/polyfill-intl-icu v1.0.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring v1.0.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php56 v1.0.0 Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions
symfony/polyfill-php70 v1.0.0 Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-util v1.0.0 Symfony utilities for portability of PHP codes
symfony/swiftmailer-bundle v2.3.9 Symfony SwiftmailerBundle
symfony/symfony v3.0.0 The Symfony PHP framework
twig/twig v1.23.1 Twig, the flexible, fast, and secure template language for PHP
This should help you see whether the update actually worked. If all looks fine, I would go the safe route and revert your code back to 2.8 and then do the update like explained below, instead of changing the composer.json!
First make sure you fixed all deprecations in your existing application. You can use the UPGRADE-document as reference, but also run your tests and check the logs for deprecated calls. This will become easier with 3.3+ as these versions have a separate deprecation-log that you can find in var/log alongside the other log files.
Once you are reasonably sure you code will run with a new major version, just use the following composer command:
composer require symfony/lts:"^3.0"
This is only to make sure that we don't accidentally install any Symfony component that is 4.0 during the process. Once you want to upgrade to Symfony 4 you can just remove this dependency using composer remove symfony/lts and then run update.
After the lts meta package is in place you can update Symfony itself:
composer require symfony/symfony:^3.0
to update to the newest 3.x that your dependencies support or use a stricter constraint if you really want to go step by step:
composer require symfony/symfony:3.0.*
You can also do both things in one step if you like:
$ composer require symfony/lts:^3.0 symfony/symfony:^3.0
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 5 installs, 2 updates, 0 removals
- Updating symfony/symfony (v2.8.32 => v3.4.2): Downloading (100%)
- Installing psr/simple-cache (1.0.0): Loading from cache
- Installing psr/link (1.0.0): Loading from cache
- Installing psr/container (1.0.0): Loading from cache
- Installing psr/cache (1.0.1): Loading from cache
- Installing fig/link-util (1.0.0): Loading from cache
Writing lock file
Generating autoload files
> Incenteev\ParameterHandler\ScriptHandler::buildParameters
Updating the "app/config/parameters.yml" file
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache
// Clearing the cache for the dev environment with debug
// true
[OK] Cache for the "dev" environment (debug=true) was successfully cleared.
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets
Trying to install assets as relative symbolic links.
[OK] No assets were provided by any bundle.
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installRequirementsFile
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::prepareDeploymentTarget
This should already be enough to have Symfony on the newer version. You will likely have other dependencies such as Doctrine or some bundles that you have installed as well. There are a few useful commands for updating them.
First you can update only a single dependency at a time:
composer update doctrine/orm
When you add the option --with-dependencies it will also update doctrine/orm's dependencies.
You can always ask composer why or composer why-not with a dependency and optionally a version to check why a dependency is there and why it won't update. Just type composer help why-not to see how to use it. This is particularly helpful when you want to update a dependency like doctrine/orm, but composer throws an error that it can't do it.

PHPWord through composer on wamp

I know there are many subject about composer but I won't be able to install phpword through composer.
Basically I need to install PHPWord to create word on my network server.
I install wamp on my computer, install composer and create a new folder on www.
Let's say the main directory is : www/test/
I write a composer.json file :
{
"require": {
"phpoffice/phpword": "v0.13.*"
}
}
And with command line, I tried composer install :
Failed loading c:/wamp/bin/php/php5.6.19/ext/php_xdebug-2.4.0-5.6-vc11-nts.dll
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 6 installs, 0 updates, 0 removals
- Installing pclzip/pclzip (2.8.2): Loading from cache
- Installing phpoffice/common (v0.2.6): Loading from cache
- Installing zendframework/zend-stdlib (2.4.13): Loading from cache
- Installing zendframework/zend-validator (2.4.13): Loading from cache
- Installing zendframework/zend-escaper (2.4.13): Loading from cache
- Installing phpoffice/phpword (v0.13.0): Loading from cache
zendframework/zend-stdlib suggests installing zendframework/zend-eventmanager (To support aggregate hydrator usage)
zendframework/zend-stdlib suggests installing zendframework/zend-serializer (Zend\Serializer component)
zendframework/zend-stdlib suggests installing zendframework/zend-servicemanager (To support hydrator plugin manager usage)
zendframework/zend-stdlib suggests installing zendframework/zend-filter (To support naming strategy hydrator usage)
zendframework/zend-validator suggests installing zendframework/zend-db (Zend\Db component)
zendframework/zend-validator suggests installing zendframework/zend-filter (Zend\Filter component, required by the Digits validator)
zendframework/zend-validator suggests installing zendframework/zend-i18n (Zend\I18n component to allow translation of validation error messages as well as to use the various Date validators)
zendframework/zend-validator suggests installing zendframework/zend-math (Zend\Math component)
zendframework/zend-validator suggests installing zendframework/zend-resources (Translations of validator messages)
zendframework/zend-validator suggests installing zendframework/zend-servicemanager (Zend\ServiceManager component to allow using the ValidatorPluginManager and validator chains)
zendframework/zend-validator suggests installing zendframework/zend-session (Zend\Session component)
zendframework/zend-validator suggests installing zendframework/zend-uri (Zend\Uri component, required by the Uri and Sitemap\Loc validators)
phpoffice/phpword suggests installing ext-gd2 (Allows adding images)
phpoffice/phpword suggests installing dompdf/dompdf (Allows writing PDF)
Writing lock file
Generating autoload files
So now I've got a new folder call vendor with autoload.php and many folder inside (zendframework, phpoffice...)
When I try to go to a phpword example, I've got this error :
Fatal error: Uncaught exception 'Exception' with message 'Could not find file '/autoload.php'. It is generated by Composer. Use 'install --prefer-source' or 'update --prefer-source' Composer commands to move forward.' in C:\wamp\www\Nouveau dossier (2)\vendor\phpoffice\phpword\bootstrap.php on line 22
( ! ) Exception: Could not find file '/autoload.php'. It is generated by Composer. Use 'install --prefer-source' or 'update --prefer-source' Composer commands to move forward. in C:\wamp\www\Nouveau dossier (2)\vendor\phpoffice\phpword\bootstrap.php on line 22
The path to autoload.php seems ok.
Anyone know why there is this problem ?
After I juste need to copy vendor folder to my network server ? (windows server with wamp)
Thank for your help.

Dependencies not installing correctly during Heroku deploy?

I'm trying to deploy a PHP app to Heroku but I don't think my dependencies are installing properly.
When I run git push heroku master I see these lines:
Installing dependencies...
Composer version 1.2.4 2016-12-06 22:00:51
Loading composer repositories with package information
Installing dependencies from lock file
-Installing components/jquery (3.1.1)
Downloading: 100%
-Installing fortawesome/font-awesome (v4.7.0)
Downloading: 100%
-Installing phpmailer/phpmailer (v5.2.22)
Downloading: 100%
No errors or anything. But when I open the heroku app after it's been deployed I have all kinds of console errors for jquery not being found.
I looked in developer tools in the sources tab and see a folder named vendor/fortawesome/font-awesome/css (that's the actual whole folder name) with the file font-awesome.min.css in it ... but shouldn't they all be installed in a vendor folder?
Not sure what I'm doing wrong.

Why does Composer Install a Different Package?

I've been working with the Magento Firegento custom Composer installer, and I ran into this odd bit of Composer behavior I don't understand.
Consider the following dead simple composer.json file
{
"require": {
"magento-hackathon/magento-composer-installer": "*"
}
}
If I run compser.phar install with this composer.json file, I get the following.
$ composer.phar install --no-dev
Loading composer repositories with package information
Installing dependencies
- Installing aoepeople/composer-installers (v0.0.1)
Loading from cache
Writing lock file
Generating autoload files
From my mostly lay-person's understanding composer.phar, I've said
Hey, composer, please install the magento-hackathon/magento-composer-installer package from packagist.org
And composer has said back to me
Sir, yes sir! Here's the aoepeople/composer-installers package
I don't understand why composer installed aoepeople/composer-installers, when I asked for magento-hackathon/magento-composer-installer.
To be clear: I understand the reason magento-hackathon/magento-composer-installer wasn't installed is this is a package that lives in a different composer repository. My original mistake was not including this repository in my composer.json file.
However, it doesn't make sense to me that composer would install a different package than the one I asked for. When I search packagist there's no magento-hackathon/magento-composer-installer extension.
Why does packagist install a different extension? What's happening behind the scenes to make magento-hackathon/magento-composer-installer resolve to aoepeople/composer-installers? How/where in the composer source could I debug this sort of thing myself in the future?
Vinai published a great writeup on Magento and Composer here.
Quoting from there
Composer will use packagist.org to look where to get the libraries. Because Magento modules aren’t listed there, you will have to add the packages.firegento.org repository to the configuration as follows [...]
So you will need the repository
"repositories":[
{
"type":"composer",
"url":"http://packages.firegento.com"
}
],
to get the magento composer installer.
And yes composer offers replacements. On the packaging entry for aoepeople/composer-installers you will notice the replaces section:
And since magento-hackathon/magento-composer-installer is not available on packagist composer will deliver you aoepeople/composer-installers.
OK, so that's a weird behavior :) I'm the author of the aoepeople/composer-installers package and the idea behind this is that this package provides an alternative (very basic and simplified implementation of the composer package-type magento-module - and adds another type magento-source. I only want the installer to put the package in the right place - keeping it simple. That's why I decided to come up with an alternative package.
The reason why the aoepeople/composer-installer replaces the magento-hackathon/magento-composer-installer is because many Magento modules already come with a composer.json that requires the magento-hackathon/magento-composer-installer. In order to be able to seamlessly use the simpler installer that one is "pretending" to be the hackathon-installer. But unless you actively require aoepeople/composer-installers in your project's composer.json you should continue using the original installer as no Magento module's composer.json out there (not even ours) is directly referring to aoepeople/composer-installers.
The fact that packagist tries to be "smart" and returns a package that replaces a package that's not registered is new to me and - to be honest - very disturbing. While this might be intended behavior I share the opinion that this could easily be abused. I start liking Alan's idea to bypass packagist completely using "packagist":"disabled". Especially in the case of Magento modules this seems to happen easily because most of the Magento modules are unknown to packagist while being registered at packages.firegento.com only.
A simple quickfix/workaround to this specific situation could be to register magento-hackathon/magento-composer-installer on packagist.org.
The Packagist package at https://packagist.org/packages/aoepeople/composer-installers has metadata saying that it replaces the magento-hackathon/magento-composer-installer package. Packagist would then install this as it's the package that replaces what you asked for.
Docs here: https://getcomposer.org/doc/04-schema.md#replace

Laravel 4 logic?

I recently downloaded a version of Laravel 4 and it was only 40kb. Laravel 3.2 was about 3.5mb, my questions are:
How can I use Laravel 4 completely offline?
I found out, Laravel 4 core codes was missing, Does it work cloud base or something like it!?
Which version do you recommend to use? Laravel 3.2 or Laravel 4? ( mostly for local developing )
thanks.
How did you installed Laravel? Using composer is as simple as:
composer create-project laravel/laravel your-project-name --prefer-dist
I use Laravel 4 for local development, and it works like a charm!
That's mainly because Laravel 4 uses composer to handle dependencies. What you downloaded wasn't exactly Laravel, but rather the structure for it. It's a preset project, with all default Laravel dependencies, so that all you have to do is run a command and download it all.
To use Laravel 4, you'll need to install composer. After you're done, open your command line, go to the folder where you saved the Laravel files you downloaded and, inside it, type: php composer install. This will download all the dependencies needed for the project; that means Laravel files and all of its own dependencies. It may take a while to install, and may seem to be stuck at Installing dependencies (including require-dev) for quite a long time, since there are lots of dependencies to be fetched, but that's normal. After it's done, you should see something like this:
Loading composer repositories with package information
Installing dependencies (including require-dev)
- Installing doctrine/lexer (dev-master bc0e1f0)
Downloading: 100%
...
- Installing laravel/framework (4.0.x-dev 733492c)
Downloading: 100%
...
Writing lock file
Generating autoload files
Generating optimized class loader
Now all you have to do is point the root of your webserver to the /public folder and start programming. If you ever feel you want to update your dependencies, simply run composer update.
Note: Remember to enable PHP's openssl extension, so composer can download the projects from github, and Apache's mod_rewrite, so Laravel pretty URLs work. If you're using Apache, that is.
Note²: Whenever you create a new command, controller, model, migration or seed, you'll have to type composer dump-autoload on your console, so composer knows how to autoload it.

Categories