laravel 5,am I including too much libraries? - php

I am new to Laravel, and I have started working on Laravel 5.x( the current version on the question date). My app uses bunch of libraries, including Amazon MWS library, a parser and many more, see this image for an idea
these seems to be a lot of folder, and including Laravel Framework folder...
many of these built in options would never by used by me directly and intentionally..Also, say from a library, I would use only one method (tojson method from a lib for example)
Already admitted, I am new to Laravel, and from my native understanding, only parts of libraries are included when called..
So, my basic question is,
When and HOW do you know you are including too much ?
How you people manage this??
thanks...and sorry if my question seems offensive to anyone, I believe its directly relating to programming and will help many others like me too..

There's no problem with including many libraries.
Requiring and installing libraries doesn't create overhead in itself, as that code is executed only when you use it. Also most of the libraries in the vendor directory are Laravel dependencies.
If you look at the framework's composer.json file and you'll see that Laravel already has a lot of dependencies as it uses a lot of Symfony components for the core, Monolog for logging, SwiftMailer for sending emails, and the list goes on:
"require": {
"php": ">=5.5.9",
"ext-mbstring": "*",
"ext-openssl": "*",
"classpreloader/classpreloader": "~3.0",
"doctrine/inflector": "~1.0",
"jeremeamia/superclosure": "~2.2",
"league/flysystem": "~1.0",
"monolog/monolog": "~1.11",
"mtdowling/cron-expression": "~1.0",
"nesbot/carbon": "~1.20",
"paragonie/random_compat": "~1.4",
"psy/psysh": "0.7.*",
"swiftmailer/swiftmailer": "~5.1",
"symfony/console": "2.8.*|3.0.*",
"symfony/debug": "2.8.*|3.0.*",
"symfony/finder": "2.8.*|3.0.*",
"symfony/http-foundation": "2.8.*|3.0.*",
"symfony/http-kernel": "2.8.*|3.0.*",
"symfony/polyfill-php56": "~1.0",
"symfony/process": "2.8.*|3.0.*",
"symfony/routing": "2.8.*|3.0.*",
"symfony/translation": "2.8.*|3.0.*",
"symfony/var-dumper": "2.8.*|3.0.*",
"vlucas/phpdotenv": "~2.2"
},
Just install whatever you need, there's no limit to how many libraries you use as long as your code is written properly.

Related

Module (Laminas\Crypt) could not be initialized

I want to use Bcrypt of Laminas-Crypt package in one of my files. After running
composer require laminas/laminas-crypt,
composer dump-autoload,
composer update
I declared a use statement in my controller:
use Laminas\Crypt\Password\Bcrypt;
The application loaded without any errors, but as far as I am concerned, all namespaces should be registered in config/modules.config.php for the autoloader to load the namespaces accordingly. After adding Laminas\Crypt namespace to the folder the application threw an exception:
Uncaught Laminas\ModuleManager\Exception\RuntimeException: Module (Laminas\Crypt) could not be initialized. in C:\xampp\htdocs\phpLessons\Shortly\vendor\laminas\laminas-modulemanager\src\ModuleManager.php:180
My question is:
Can the application function properly without the namespace of the used package being defined in the config/modules.config.php of root directory?
How can the exception be handled?
Edit:
Here is the relevant part of composer.json
"require": {
"php": ">=8.1.0",
"laminas/laminas-component-installer": "^3.0",
"laminas/laminas-development-mode": "^3.2",
"laminas/laminas-skeleton-installer": "^1.0",
"laminas/laminas-mvc": "^3.3.4",
"laminas/laminas-db": "^2.12.0",
"laminas/laminas-mvc-form": "^2.0.0",
"laminas/laminas-json": "^3.2",
"laminas/laminas-log": "^2.13.1",
"laminas/laminas-cli": "^1.1.1",
"laminas/laminas-mvc-i18n": "^1.2.0",
"laminas/laminas-mvc-plugins": "^1.1.0",
"laminas/laminas-mvc-middleware": "^2.0.0",
"laminas/laminas-session": "^2.10.0",
"laminas/laminas-di": "^3.2.2",
"laminas/laminas-view": "^2.25",
"laminas/laminas-form": "3.7",
"laminas/laminas-crypt": "^3.9",
"ext-pdo": "*",
"laminas/laminas-mail": "^2.20",
"doctrine/orm": "^2.14",
"doctrine/dbal": "^3.2",
"symfony/yaml": "^5.4",
"symfony/cache": "^5.4",
"doctrine/common": "^3.4",
"doctrine/doctrine-orm-module": "^5.3"
},
and modules.config.php from config/autoload:
return [
'Laminas\Cache',
'Laminas\Paginator',
'Laminas\Mail',
'Laminas\Mvc\Plugin\FilePrg',
'Laminas\Mvc\Plugin\FlashMessenger',
'Laminas\Mvc\Plugin\Identity',
'Laminas\Mvc\Plugin\Prg',
'Laminas\Session',
'Laminas\Mvc\Middleware',
'Laminas\Mvc\I18n',
'Laminas\Form',
'Laminas\I18n',
'Laminas\Log',
'Laminas\InputFilter',
'Laminas\Filter',
'Laminas\Hydrator',
'Laminas\Di',
'Laminas\Db',
'Laminas\Router',
'Laminas\Validator',
'Laminas\DeveloperTools',
'Laminas\Diactoros',
'DoctrineModule',
'DoctrineORMModule',
'Application',
'User',
'Pages',
];

Adding a composer package to Laravel 5

"require": {
"infusionsoft/php-sdk": "^1.2",
"laravel/lumen-framework": "5.1.*",
"vlucas/phpdotenv": "~1.0",
"barryvdh/laravel-debugbar": "^2.2",
"goaop/framework": "^2.0",
"php-http/httplug": "^1.1",
"infusionsoft/old-php-isdk": "*"
},
I'm trying to use the "infusionsoft/old-php-isdk" which is in the packagist composer repository but it's not specific to Laravel. I'm using an order controller and it's code that I picked up from another developer so he references the current (and working) "infusionsoft/php-sdk" as the following
"use infusionsoft\infusionsoft;"
What namespace do I call it if it has no namespace from the package?
Here's the original link to the composer package.
https://packagist.org/packages/infusionsoft/old-php-isdk
Am I missing something? Do I need to assign it a namespace? What and where would I put it?

mb_substr() undefined Facebook canvas - Heroku server

The problem is that when I open the facebook canvas. I get this error. "call to undefined function mb_substr"
The solution that most people suggest is to put "ext-mbstring" as a require. I did that in different way's. This is what I have now:
{
"require" : {
"silex/silex": "~1.3",
"monolog/monolog": "~1.7",
"twig/twig": "^1.19",
"symfony/twig-bridge": "^2.7"
},
"require-dev": {
"heroku/heroku-buildpack-php": "*"
},
"require": {
"ext-mbstring": "*"
}
}
This does not work for me. I have also checked the php info of my heroku server.
This is the output. As you can see the mbstring is set.
What I at last think the problem could be is the php version. Facebook wants a version that's higher than 5.4. I am using version 5.6.15.
So that shouldn't be the problem either.
Anyone knows what I missed?
Since it's a JSON object and require is the property key, I don't think you can just add a second property with the same key. Try to add the dependency to the first require property.
{
"require" : {
"php": ">=5.6.0",
"ext-mbstring": "*",
"silex/silex": "~1.3",
"monolog/monolog": "~1.7",
"twig/twig": "^1.19",
"symfony/twig-bridge": "^2.7"
},
...
Don't forget to run composer update after you make changes to composer.json.

Is this composer.json correct should it have dev-master in it

I keep on getting error whenever update should i keep dev.. today as i updated i got another error for sonata admin which is discused https://groups.google.com/forum/#!topic/sonata-users/4AGVPyBMTSI
my only question is it a good practise to keep dev in composer.json because all the lib which i go to install show dev-master in their documentation. I know this is very basic but i am new to composer and symfony2.. and not enjoying it a bit hahaha
{
"name": "symfony/framework-standard-edition",
"license": "MIT",
"type": "project",
"description": "The \"Symfony Standard Edition\" distribution",
"autoload": {
"psr-0": { "": "src/" }
},
"require": {
"php": ">=5.3.3",
"symfony/symfony": "~2.4",
"doctrine/orm": "~2.2,>=2.2.3",
"doctrine/doctrine-bundle": "~1.2",
"twig/extensions": "~1.0",
"symfony/assetic-bundle": "~2.3",
"symfony/swiftmailer-bundle": "~2.3",
"symfony/monolog-bundle": "~2.4",
"sensio/distribution-bundle": "~2.3",
"sensio/framework-extra-bundle": "~3.0",
"sensio/generator-bundle": "~2.3",
"incenteev/composer-parameter-handler": "~2.0",
"friendsofsymfony/user-bundle": "1.3.*",
"sonata-project/core-bundle": "~2.2#dev",
"sonata-project/admin-bundle": "2.2.*#dev",
"sonata-project/block-bundle": "dev-master",
"sonata-project/cache": "1.*#dev",
"sonata-project/cache-bundle": "2.2.*#dev",
"sonata-project/jquery-bundle": "1.8.*#dev",
"sonata-project/easy-extends-bundle" : "dev-master",
"sonata-project/doctrine-orm-admin-bundle": "dev-master",
"sonata-project/user-bundle": "dev-master"
},
"scripts": {
"post-install-cmd": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
],
"post-update-cmd": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
]
},
"config": {
"bin-dir": "bin"
},
"extra": {
"symfony-app-dir": "app",
"symfony-web-dir": "web",
"incenteev-parameters": {
"file": "app/config/parameters.yml"
},
"branch-alias": {
"dev-master": "2.4-dev"
}
}
}
You should avoid using named branches like dev-master if you don't actively develop that other software and know what's going on there.
Here's why: Today that branch might be the tip of the development of the 2.x version of a component. You happily update with Composer, and everything works fine.
Now that software decides that there will be a new major version released, and they start making backwards-incompatible changes. Now your own software that uses dev-master will point at that old commit of 2.x as long as you don't update anymore (or only update OTHER software explicitly), but in the long run things will break:
Because yet other components that you add might depend on that software in dev-master, but THEY mean the new 3.x development, your old software would satisfy that dependency, but not the code that is connected with it (remember: backwards-incompatible changes may also be forward-incompatible changes). So even without updating that particular component, things may at some point start breaking.
And if you blindly update the whole thing, you are even more likely to run into problems if such an update occurs.
Using version numbers in the dependencies is much easier. You can declare to require any version 2.*, which will prevent the update to any 3.x version, and it allows Composer to detect that a third component that depends on that 3.x version is incompatible with the older 2.x that is still installed. It will either lead to picking an older version that still depends on 2.x, or refuse to install it altogether. Which actually is a good thing because it allows you to manually update and see if things break (you do have tests, don't you?) - you can then fix them.
If you encounter a software you like, and the maintainers haven't yet tagged a version, please ask them nicely to do so. Either they respond with a tag, or you get the idea to search for another library that better suits your needs. Otherwise, expect to implicitly be part of their development team and help debugging their software (which can also be a good thing - but be prepared to spend that extra amount of time).

How do I load the Paypal SDK into Silex?

I'm having some trouble. I'm using Silex, and I'm trying to load the Paypal SDK into it. I think my issue is related to my understanding of Silex, which is why I'm posting it here.
My Setup
Paypal's developer website says to add a few lines to the composer file (PHP tab). So, I've done that. My new composer file looks like so (the last four entries were those suggested by Paypal):
{
"require": {
"silex/silex": "1.0.*#dev",
"symfony/browser-kit": "2.1.*",
"symfony/console": "2.1.*",
"symfony/css-selector": "2.1.*",
"symfony/dom-crawler": "2.1.*",
"symfony/filesystem": "2.1.*",
"symfony/finder": "2.1.*",
"symfony/form": "2.1.*",
"symfony/locale": "2.1.*",
"symfony/process": "2.1.*",
"symfony/security": "2.1.*",
"symfony/serializer": "2.1.*",
"symfony/translation": "2.1.*",
"symfony/validator": "2.1.*",
"symfony/monolog-bridge": "2.1.*",
"symfony/twig-bridge": "2.1.*",
"monolog/monolog": ">=1.0.0,<1.2-dev",
"twig/twig": ">=1.2.0,<2.0-dev",
"doctrine/dbal": ">=2.2.0,<2.4.0-dev",
"swiftmailer/swiftmailer": "4.1.*",
"php": ">=5.3.0",
"ext-curl": "*",
"ext-json": "*",
"paypal/rest-api-sdk-php" : "0.7.*"
}
}
Then, I ran composer update, and it updated everything. I checked in the vendor folder, and a paypal folder had been created and seemed to be good to go.
The Problem
Unfortunately, even though I've done the above steps, it still isn't working. I'm following the beginners steps outlined on Paypal's website, it seems that one of the Paypal classes can't be located.
The line that causes the error is taken directly from the "beginners steps" page linked to above. For quick reference, I'll provide it below as well:
$cred = new OAuthTokenCredential("AQkquBDf1zctJOWGKWUEtKXm6qVhueUEMvXO_-MCI4DQQ4-LWvkDLIN2fGsd","EL1tVxAjhT7cJimnz5-Nsx9k2reTKSVfErNQF-CmrwJgxRtylkGTKlU4RvrX", $sdkConfig);
Unfortunately that line causes an error that says:
Fatal error: Class 'OAuthTokenCredential' not found in ...
Summed up...
The problem seems to be that Silex isn't loading--or making available--the class OAuthTokenCredential. What do I do?
Thanks Laurynas MaliĊĦauskas and user2930475 for this answer (in the comments of my post).
Looks like I had neglected to put a line such as:
use PayPal\Auth;
in my app.php file.
Once added, I also needed to change my new class line to this:
$cred = new PayPal\Auth\OAuthTokenCredential("AQkquBDf1zctJOWGKWUEtKXm6qVhueUEMvXO_-MCI4DQQ4-LWvkDLIN2fGsd","EL1tVxAjhT7cJimnz5-Nsx9k2reTKSVfErNQF-CmrwJgxRtylkGTKlU4RvrX", $sdkConfig);
Note that I have all of my controllers actually in the app.php file.
Actually, you don't need to add anything to app.php at all. Just do this:
use PayPal\Auth\OAuthTokenCredential.php;
class YourController{
public function myAction(){
// ...
$cred = new OAuthTokenCredential("AQkquB...","EL1tVx...", $sdkConfig);
// ...
}
}
You should never need to modify app.php just to use a class.

Categories