Laravel package development how do I use another facade in my package - php

I am creating my own Laravel package, in my package I have some views and I want to use the Form facade in one of my views, something like this: Form::open(). Is this possible, if so, how?
This is my composer.json:
{
"name": "thomasbird/laravel-cms",
"description": "A basic content management package that encapsulates some of the common functionality of a cms",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "Thomas Bird",
"email": "godoploid#gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"laravelcollective/html": "5.*",
"symfony/css-selector": "^3.2"
},
"require-dev": {
"symfony/dom-crawler": "2.8.*|3.0.*"
}
}

You need to specify any required packages within the require section of your composer.json.
For example, if you wish to use laravel collective's html helpers, in your package's composer.json, you can specify as follows:
"require": {
"php": "^7.0",
"laravelcollective/html":"^5.4.0",
...
...
...
So you can use any facades included within your required packages.

Related

Composer not installing dependencies of Package

I have two projects. One is my application and the second one is an external module that I want to use in future apps.
I have created my external module on GitHub and included in the composer.json of my application.
My external module gets downloaded / cloned but the required dependencies are not installed by composer.
Here's composer.json of my application:
{
"name": "application",
"description": "Skeleton Application for ZF2",
"license": "BSD-3-Clause",
"keywords": [
"framework",
"zf2"
],
"homepage": "http://framework.zend.com/",
"require": {
"php": ">=5.5",
"zendframework/zendframework": "2.*",
"zf-commons/zfc-user": "1.4.4",
"doctrine/doctrine-orm-module": "~0.9.2",
"zf-commons/zfc-user-doctrine-orm": "1.0.*",
"zendframework/zend-developer-tools": "^0.0.2",
"username/GlideUser": "dev-master"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/username/GlideUser.git"
}
]
}
Here's composer.json of my external module:
{
"name": "username/glide-user",
"description": "Module For Zend Framework 2",
"type": "library",
"license": "BSD-3-Clause",
"homepage": "https://github.com/username/GlideUser",
"keywords": [
"zf2",
"zfc-user",
"bjyauthorize"
],
"authors": [
{
"name": "Haris Mehmood",
"email": "abc#outlook.com",
"homepage": "abc.com",
"role": "Developer"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=5.3.3",
"bjyoungblood/bjy-authorize": "1.4.0"
},
"autoload": {
"psr-0": {
"GlideUser\\": "src/"
}
}
}
When I run composer install or composer update I expect bjyauthorize package to gets installed, but composer ignore the dependency and install everything else.
What is it that I am doing wrong here.
Try updating the following line in your application composer.json from:
"username/GlideUser": "dev-master"
to:
"username/glide-user": "dev-master"
This way, what is being required matches the name of the external module, which is the name defined inside the external module's composer.json.
Although it does not look directly applicable to the dependencies of the external module not installing, it could be the cause.

How to register a dependency when creating laravel package

I would like to know how to register a dependency when creating laravel package.
In my package composer.json I have :
{
"name": "facilinfo/gallery",
"description": "Photo galleries management package for laravel",
"type": "library",
"license": "MIT",
"keywords": ["laravel"],
"authors": [
{
"name": "facilinfo",
"email": "contact#facil-info.net"
}
],
"minimum-stability": "stable",
"require": {
"laravelcollective/html": "5.2.*",
"intervention/image": "dev-master"
},
"autoload": {
"psr-4": {
"Facilinfo\\Gallery\\": "src/"
}
}
}
And when I do
$form = new Form();
I have a Class not found error.
How can I solve it?
Finally, I found the soultion myself. I had tout add this to my package service provider register function:
$this->app->register(\Collective\Html\HtmlServiceProvider::class);
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Form', '\Collective\Html\FormFacade');
and to add "laravelcollective/html": " 5.2.*" in the laravel installation where I'm developping my package and install in. It works now.

PHP & Composer, how do I combine composer.json files

Can someone explain how I'm supposed to use composer with a php. I have a composer.json file in my doc root that downloads the core packages for my project, but then when I want to add another project like google+ php sdk found here https://github.com/googleplus/gplus-quickstart-php/
what do I do with that composer.json file? do i combine them manually? Do I just download that composer.json into a different dir?
my current composer.json file looks like this
{
"name": "fuel/fuel",
"type": "metapackage",
"description": "The FuelPHP framework",
"keywords": ["framework"],
"homepage": "http://fuelphp.com",
"license": "MIT",
"authors": [
{
"name": "FuelPHP Development Team",
"email": "team#fuelphp.com"
}
],
"support": {
"irc": "irc://irc.freenode.org/fuelphp",
"forum": "http://fuelphp.com/forums"
},
"require": {
"php": ">=5.3.3",
"monolog/monolog": "1.5.*",
"fuelphp/upload": "2.0",
"omissis/php-cloudfiles": "dev-master",
"mustache/mustache": "*"
},
"suggest": {
"mustache/mustache": "Allow Mustache templating with the Parser package",
"smarty/smarty": "Allow Smarty templating with the Parser package",
"twig/twig": "Allow Twig templating with the Parser package",
"mthaml/mthaml": "Allow Haml templating with Twig supports with the Parser package"
},
"config": {
"vendor-dir": "fuel/vendor"
},
"minimum-stability": "dev"
}
The g+ composer.json file looks like
{
"name": "googleplus/quickstart",
"description": "This quick-start app is built in PHP and lets you get started with the Google+ platform in a few minutes.",
"license": "Apache-2.0",
"repositories": [
{
"type": "package",
"package": {
"name": "google/google-api-php-client",
"version": "0.6.2",
"dist": {
"url": "http://google-api-php-client.googlecode.com/files/google-api-php-client-0.6.2.tar.gz",
"type": "tar"
},
"autoload": {
"classmap": [
"src/"
]
}
}
}
],
"require": {
"silex/silex": "1.0.*#dev",
"twig/twig": ">=1.8,<2.0-dev",
"google/google-api-php-client": "0.6.2"
}
}
Simply add this into the require block of your original composer file:
"googleplus/quickstart": "*",
I have little experience with Composer, but I can tell you that when you require a lib, you add that lib to your main composer.json file. Upon installation or update, the new lib will be downloaded and its composer.json file will be read by Composer; its dependencies will be automatically downloaded, and so on.
So we could say you're not supposed to download your requirements manually, you have to use Composer for that, it will take care of it for you.

ZF2 Modules & external dependencies

I'm not clear on what the best practice is for including external libraries in a custom module that I plan on distributing.
Normally I'd place the external library under the application's vendor directory. But I want to ensure that all dependencies are met when I distribute my custom module (i.e. I don't want to force people to manually download the dependency into their app's vendor directory).
Is the correct practice to include a vendor directory under the module directory as per the the following?
/application_dir
/vendor
/module
/my_module
/vendor
Use composer, you can find documentation here:
Composer Documentation
Basically you would modify the file composer.json in the root of your application and add your dependencies in the require section:
{
"name": "zendframework/skeleton-application",
"description": "Skeleton Application for ZF2",
"license": "BSD-3-Clause",
"keywords": [
"framework",
"zf2"
],
"homepage": "http://framework.zend.com/",
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": "2.2.*",
"doctrine/common": "dev-master",
"zendframework/zendpdf": "2.*",
"zendframework/zendservice-recaptcha": "2.*",
"thiagoalessio/tesseract_ocr": ">= 0.1.2",
"zf-commons/zfc-user": "dev-master"
// add your requirements here**
}
}
if the dependancy is on a private github repository, you can add it like this:
{
"require": {
"vendor/my-private-repo": "dev-master"
},
"repositories": [
{
"type": "vcs",
"url": "git#bitbucket.org:vendor/my-private-repo.git"
}
]
}
Don't forget to do composer.phar update after your done adding them.

How to install your own bundle with Composer in Symfony 2.1?

I've just moved to Symfony 2.1, and I can't understand, how can I install my own bundles with Composer?
It was very easy in 2.0.x in deps:
[MyOwnBundle]
git=git#git.weboshin.ru:weboshin_cms_bundle.git
target=/bundles/My/OwnBundle
After that I just triggered bin/vendors update and that was it!
But now there's no deps file, and I supposed to do everything with Composer. Please give me any hints.
I've found the answer.
// my_project/compose.json:
{
"repositories": [
{
"type": "vcs",
"url": "own_repository_url"
}
],
// ...
"require": {
// ...
"own/bundle": "dev-master"
}
},
 
// own_repository/own_bundle/compose.json:
{
"name": "own/bundle"
}
Add a composer.json file to your bundle. For example I have this for one of my bundles:
{
"name": "cg/kint-bundle",
"type": "symfony-bundle",
"description": "This bundle lets you use the Kint function in your Twig templates. Kint is a print_r() replacement which produces a structured, collapsible and escaped output",
"keywords": ["kint", "debug", "symfony", "bundle", "twig"],
"homepage": "http://github.com/barelon/CgKintBundle",
"license": "MIT",
"authors": [
{
"name": "Carlos Granados",
"homepage": "http://github.com/barelon"
},
{
"name": "Symfony Community",
"homepage": "http://github.com/barelon/CgKintBundle"
}
],
"require": {
"php": ">=5.3.2",
"symfony/framework-bundle": ">=2.0.0",
"raveren/kint": "dev-master"
},
"minimum-stability": "dev",
"autoload": {
"psr-0": {
"Cg\\KintBundle": ""
}
},
"target-dir": "Cg/KintBundle"
}
Then add your bundle to packagist.org. It is very simple, basically you just have to provide your git address and it will do the rest.
Once your bundle is available in packagist, then just add it as a dependency in the composer.json file for your symfony project. In my case I have:
"require": {
....
"cg/kint-bundle": "*"
},
Then just run "composer update" in your symfony directory and that´s all! You don´t even need to update the autoload file, composer will do it for you. The only thing left would be to load the bundle in appkernel.php

Categories