Laravel : best place to create a composer package - laravel

I'm going to create my first laravel based composer package . also github for windows is installed . laravel is located in :
E:\xampp\htdocs\pnu
as i need to test my package with laravel during the development(check relational paths , error debugging) where is the best place to create package root folder ?

There is no default location for this. Lets say your packages is called HelloWorld, than you can put your package here:
E:\xampp\htdocs\pnu\packages\Alex\Helloworld
And then add this to your composer file (composer.json in root directory):
"autoload": {
"psr-4": {
"Alex\\Helloworld\\": "packages/Alex/Helloworld/src"
}
},
You can read more about this here https://laracasts.com/discuss/channels/tips/developing-your-packages-in-laravel-5.

Related

Laravel package class not found

I have installed laravel packages on staging server and packages working fine. But when i take pull on staging server, it is showing me error that package class not found.
Steps I have followed to resolve issue
I have check in vendor folder as well as in config/app.php, but I got class declaration and package folder is there.
After this when I update composer, my issue get resolved.
Is there any other file which should i look for class defination?
Perform a composer update, then composer dump-autoload.
If the above doesn't solve the problem, change the classmap in your composer.json file such that it contains the project-relative path to your php files:
"autoload-dev": {
"classmap": [
"tests/TestCase.php",
"database/seeds/UserTableSeeder.php" //include the file with its path here
]},
and soon after, perform a composer dump-autoload, and it should work now

Create own laravel package

I try to create own laravel package.
I created package folder in my project in root directory.
Than created simplexi/greetr/src folders in package.
I added to autoload "Simplexi\Greetr\": "package/simplexi/greetr/src" in composer.json in main project, and used command composer dump-autoload.
Than in src folder I created RiakServiceProvider, added this provider to config=>app.php to providers array.
Then in boot method I added next code:
$this->publishes([__DIR__ . '../config/myconf.php' => config_path() . '/']);
And executed next command:
php artisan vendor:publish --provider="\Simplexi\Greetr\RiakServiceProvider"
Than I got Publishing complete.
But file myconf.php didn't copy to app/config.
Also I checked file myconf.php in my package/simplexi/greetr/config folder and it exists.
Can anyone tell me what the problem might be?
publishes() expect two parameters. Try something like this:
$this->publishes([
__DIR__.'/../config/myconf.php' => config_path('myconf.php'),
], 'config');

Can't require_once fdpf/fpdf.php

I am using the FPDI library from JanSlabon for securing PDF file uploads from my laravel app. But I can't execute the code require_once even though I navigated to the file itself. I am getting the error:
Failed opening required '../../vendor/setasign/fpdf/fpdf.php' (include_path='.:/usr/local/Cellar/php/7.3.4/share/php/pear')
My require code is:
require_once('../../vendor/setasign/fpdf/fpdf.php');
require_once('../../vendor/setasign/fpdi/src/autoload.php');
When the libraries are already located in your vendor folder, you should simply make use of the autoload.php file of composer (doesn't laravel uses this by default?).
So just add the dependencies to your composer.json (if not already done):
"require": {
"setasign/fpdf": "^1.8",
"setasign/fpdi": "^2.2",
"setasign/fpdi-protection": "^2.0"
}
Update via composer update and:
<?php
use setasign\FpdiProtection\FpdiProtection;
require_once('vendor/autoload.php');
$pdf = new FpdiProtection();
...
Your relative path ../../ to vendor is probably wrong. To avoid this issue, use the Laravel base_path() helper which will provide an absolute path.
require_once(base_path('vendor/setasign/fpdf/fpdf.php'));
require_once(base_path('vendor/setasign/fpdi/src/autoload.php'));
You can autoload using composer.json. First of all, create a directory called Custom in app directory and copy fpdi directory to app/Custom.
Now in autoload section of your composer.json file, require the file. After requiring the file, your composer.json file's autoload block should look like this if it is a fresh Laravel app:
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/Custom/fpdi/FPDI_Protection.php"
]
},
After updating your composer.json file, run composer dumpautoload. Now you can utilize the classes in your Laravel controllers or models without requiring the files manually.
While doing tests, I see that this library uses some deprecated methods and so on. You will have to deal with it, i.e. update the code to suite your needs. But I hope that this answer will help you in a way that you will be able to use any other library as well. Do a Google search and find a more modern library if this one's fixes are too broad.

Laravel 5 - workbench migrations [duplicate]

I am having trouble to create package in Laravel 5 as workbench has been removed.
As in this thread (How create package in Laravel 5?), Goldorak suggest that we have to create our own package structure ourselves.
So, how can I create the workbench manually and get everything ready for package development?
Using the laravel Workbench package:
You can add the illuminate/workbench package in a Laravel 5 by adding to your composer.json:
"illuminate/workbench": "dev-master"
then add the WorkbenchServiceProvider into your config/app.php file:
'Illuminate\Workbench\WorkbenchServiceProvider'
Now you need to create the config/workbench.php file since it has been removed from Laravel 5:
<?php
return [
/*
|--------------------------------------------------------------------------
| Workbench Author Name
|--------------------------------------------------------------------------
|
| When you create new packages via the Artisan "workbench" command your
| name is needed to generate the composer.json file for your package.
| You may specify it now so it is used for all of your workbenches.
|
*/
'name' => '',
/*
|--------------------------------------------------------------------------
| Workbench Author E-Mail Address
|--------------------------------------------------------------------------
|
| Like the option above, your e-mail address is used when generating new
| workbench packages. The e-mail is placed in your composer.json file
| automatically after the package is created by the workbench tool.
|
*/
'email' => '',
];
Fill your information in this config file then you will be able to use the workbench command:
php artisan workbench vendor/name
Creating your own package structure
In this exemple we will create our package called awesome in a packages directory.
Here is the package structure:
packages/
vendor/
awesome/
src/
Awesome.php
composer.json
Vendor: your vendor name, typically this is your github username.
Awesome: the name of your package
src: Where you put the business logic
To generate a composer.json file you can use this command in the packages/vendor/awesome directory:
composer init
Now we create a Awesome.php class in the src directory with a simple method:
<?php namespace Vendor/Awesome;
class Awesome
{
public static function printAwesomeness()
{
echo 'Awesome';
}
}
After that we add the package to the laravel composer.json psr-4 autoloader:
"autoload": {
"psr-4": {
"App\\": "app/",
"Vendor\\Awesome\\": "packages/vendor/awesome/src"
}
},
and we dump the composer autoloader
composer dump-autoload
Now you can use your package everywhere in your laravel 5 project. If you need some laravel specific feature like service provider or view publishing, use them as described in the Laravel 5.0 documentation.
laravel 5 Standards with out workbench.
Set 1 : install laravel as usual.
Step 2 : Create package folder and service provider
In root directory create a folder call "packages" /"vendorName"/"packageName"/src" Eg: root/packages/jai/Contact/src
now navigate to src folder and create a service provider class: "ContactServiceprovider.php"
your service provider should extend ServiceProvider which has to implement register method.
Note:If you want you can have dd("testing"); in boot function and go to step 3 but you have copied the file you might want to create views , routes , config and controllers check link below for that
Step 3 : add package path in root composer.json in your root composer.json file "jai\Contact\": "packages/jai/Contact/src/" under psr-4
"psr-4": { "App\": "app/", "Jai\Contact\": "packages/jai/contact/src/", }
Step 4 : add service provider in app config.
in your root/conifg/app.php under providers add your package service provider to hook your package in.
'Jai\Contact\ContactServiceProvider',
Step 5 : run composer dump-autoload - make sure there are no errors.
all done - now you can access your package via url - "yourwebsite/contact"
Resource from here : https://github.com/jaiwalker/setup-laravel5-package
You could use package on this named packman. composer global require "hadefication/packman", just a simple package creator for Laravel.

Created package in Laravel workbench, but how to transfer to vendor folder?

Let's say my package in Laravel is test/test.
I created the package in the workbench and it's been working great following Jason Lewis' tutorial. Now I want to move the package out of
the workbench to the vendor directory. This is where all tutorials fall short, even the laravel docs. I didn't want to use git to move the files, so I simply copied the test/test package from the workbench to the vendor directory (and then deleted it from the workbench). I didn't copy the test/test/vendor folder from the workbench (or any other files I noticed in the .gitignore file). I then ran composer install from my new vendor/test/test directory. I then did a composer dump-autoload from the laravel root directory.
Now when I run my application I get an error that I did not get when the package was in the workbench:
Class 'Test\Test\TestServiceProvider' not found
(this is coming from \bootstrap\compiled.php on line 4121)
I also did a php artisan dump-autoload from the laravel root and I get this same error.
Any ideas? Or can someone lead me to a tutorial that takes the package development all the way to it's final resting point in the vendor directory?
Got it working.
I added:
"psr-0": {
"Test\\Test": "vendor/test/test/src/"
}
to the autoload section in composer.json in the laravel root directory so it looks like this:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-0": {
"Test\\Test": "vendor/test/test/src/"
}
},
If I decide to put the package on Packagist later then I might remove this from the autoload and just keep the package referenced in the "require" part of my composer.json. We'll see what happens when I get that far!
I think you can install your packages from your hard drive as from local repository like this:
"repositories": [
{
"type":"vcs",
"url":"/path/to/repo/here"
}
],
"require":{
"me/myrepo":"dev-master"
}

Resources