Properly Create a Laravel Package - laravel

I am having some issues with developing a package. I have read through several tutorials on the web and followed their advice but for some reason I still run into the same issue.
What I am trying to do: Develop a package in an active project
In my Laravel folder, I have a packages/companyname/laravel-model-notes. Inside that package, I have a composer.json file with the following:
{
"name": "companyname/laravel-model-notes",
"description": "A package to add notes to an eloquent model in Laravel.",
"type": "library",
"require": {},
"config": {
"sort-packages": true
},
"autoload": {
"psr-4": {
"CompanyName\\LaravelModelNotes\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"CompanyName\\LaravelModelNotes\\Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"CompanyName\\LaravelModelNotes\\NoteServiceProvider"
]
}
}
}
And then in my laravel composer.json file, I have added the following:
"autoload": {
"psr-4": {
"App\\": "app/",
"CompanyName\\LaravelModelNotes\\": "packages/companyname/laravel-model-notes/src"
},
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/Helpers/ThemeHelper.php",
"app/Helpers/BaseHelper.php"
]
},
When I manually add the service provider to the App.php things work, however artisan commands like publishing the migrations does not work. For some reason, Laravel does not see the package?
Is there some special way of building a package inside of a project and having that project recognize the package?
I am using Laravel 7.
Below is my service provider:
class NoteServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/config.php' => config_path('notes.php'),
], 'config');
if (! class_exists('CreateNotesTable')) {
// TODO: If tenant folder exists publish there too.
$this->publishes([
__DIR__.'/../database/migrations/create_notes_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_notes_table.php'),
], 'migrations');
}
}
}
/**
* Register the application services.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'notes');
}
}

Related

Using Composer autoload inside a ZIP package

I've been trying to get composer to generate classmaps for an API provided by a 3rd party company (OtherCompany in the example configuration) but have been unable to get it to work the way I think it should. However, I'm very new to composer so I may be completely wrong.
The situation is as follows. I have a project directory which contains the following composer.json (which does not generate classmaps for api.php):
{
"name": "company/company-library",
"description": "A useful library",
"type": "library",
"license": "GPL-3.0-or-later",
"authors": [
{
"name": "company",
"email": "hello#company.com"
}
],
"require": {
"othercompany/otherlibrary": "^1.0"
},
"config": {
"optimize-autoloader": true
},
"autoload": {
"psr-4": {
"Company\\CompanyLib\\": "src/"
}
},
"minimum-stability": "dev",
"repositories": [
{
"type": "package",
"package": {
"name": "othercompany/otherlibrary",
"type": "library",
"description": "Eases the integration of othercompany APIs into company's useful library",
"homepage": "https://otherlibraryportal.othercompany.com",
"version": "1.0",
"dist": {
"url": "../../../../repository/othercompany-otherlibrary-1.2.zip",
"type": "zip"
},
"require": {
"php": ">=5.6.1",
"phpseclib/phpseclib": "3.0.18"
},
"config": {
"optimize-autoloader": true
},
"autoload": {
"classmap": ["api.php"]
},
"minimum-stability": "dev"
}
}
]
}
The package "othercompany/otherlibrary" is contained in a ZIP file which only contains 2 files in a folder named othercompany-otherlibrary-1.2:
api.php
readme.txt
After a 'compose install', these files are stored in the directory:
vendor/othercompany/otherlibrary
The api.php file contains a number of classes which I would like to autoload. However, using the configuration above and various modifications of the classmap directive, have not been able to achieve the desired effect. Only if I move the classmap directive to the root level autoload directive in composer.json and update the path to vendor/othercompany/otherlibrary/api.php, classmaps are generated for the package my libary depends on. Snippet:
"autoload": {
"psr-4": {
"Company\\CompanyLib\\": "src/"
},
"classmap": ["vendor/othercompany/otherlibrary/api.php"]
}
Although this works and classmaps are now generated, I cannot help but think that this should not work this way as each package should independently be able to specify autoload options. If this is correct, what would be the right way to specify autoload options for the othercompany/otherlibrary package?
ADDITIONAL INFORMATION
api.php:
<?php
// Creates the API Request from the context
class APIRequest {
}
// API Response
class APIResponse {
}
// Api Method Type Constants
class APIMethodType {
}
// API Context that contain info for the API endpoint
class APIContext {
}
?>
companylibrary.php:
<?php
/**
* #package Company Library
* #author Company < hello#company.com >
* #version 1.0.0
*/
// Exit if accessed directly.
if (!defined('ABSPATH')) {
exit;
}
define('WC_CL_VERSION', '1.0.0');
require_once plugin_dir_path(__FILE__) . 'vendor/autoload.php';
/**
* Classes are correctly autoloaded if the autoload directive for the
* package the 'root' package depends on, is specified at the 'root'
* package level.
*
* If the autoload directive is specified at the package level of the
* 'sub' level of the package itself, no classmaps are generated.
*/
$public_key("othercompanypublickey");
$context = new APIContext();
$context->set_api_key('companyapikey');
$context->set_public_key($public_key);
$request = new APIRequest($context);
composer.json (which does generate classmaps for api.php):
{
"name": "company/company-library",
"description": "A useful library",
"type": "library",
"license": "GPL-3.0-or-later",
"authors": [
{
"name": "company",
"email": "hello#company.com"
}
],
"require": {
"othercompany/otherlibrary": "^1.0"
},
"config": {
"optimize-autoloader": true
},
"autoload": {
"psr-4": {
"Company\\CompanyLib\\": "src/"
},
"classmap": ["vendor/othercompany/otherlibrary/api.php"]
},
"minimum-stability": "dev",
"repositories": [
{
"type": "package",
"package": {
"name": "othercompany/otherlibrary",
"type": "library",
"description": "Eases the integration of othercompany APIs into company's useful library",
"homepage": "https://otherlibraryportal.othercompany.com",
"version": "1.0",
"dist": {
"url": "../../../../repository/othercompany-otherlibrary-1.2.zip",
"type": "zip"
},
"require": {
"php": ">=5.6.1",
"phpseclib/phpseclib": "3.0.18"
},
"minimum-stability": "dev"
}
}
]
}
Do not compress the folder containing the othercompany/otherlibrary files but instead, simply compress the files.
For a working project, please refer to GitHub:
github.com/ezoer/companylibrary

Can you please give me a link or a guide on how to add custom API, controllers, models, and migrations to the simple Laravel package I created?

Can you please give me a link or a guide on how to add custom API, controllers, models, and migrations to the simple Laravel package I created?
Laravel package development.
Here is the composer.json of the package.
"name": "test/greetr",
"description": "Greetr - A simple Laravel package.",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "Test Package",
"email": "test_package#test.com"
}
],
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Test\\Greetr\\": "src/"
}
},
"require": {}
}```
**Here is the class.**
```<?php
namespace Ac3\Greetr;
class Greetr
{
public function greet(String $sName)
{
return 'Hi ' . $sName . '! How are you doing today?';
}
}```
You can use Service Provider to load your routes, views, and database migrations. Then you can use it in your composer.json file.
"extra": {
"laravel": {
"providers": [
"YourServiceProvider"
]
}
}
For more details refer to Laravel Package Development. It provides a complete guide to developing a Laravel package from scratch to end.

TYPO3 Class Gpi\Controller\gpiController does not exist. Reflection failed

After I've installed my TYPO3 extension I faced this issue. I did the research for this issue. I found these solutions but none worked for me:
Link 1
Link 2
here is my composer
{
"name": "GlobalizationPartners/Gpi",
"type": "typo3-cms-extension",
"description": "",
"keywords": [
"TYPO3 CMS",
"extension"
],
"autoload": {
"psr-4": {
"GlobalizationPartners\\Gpi\\": "public/typo3conf/ext/gpi/Classes/"
}
},
"license": ["GPL-2.0-or-later"],
"require": {
"typo3/cms-core": ">=8.7.0,<11.0"
},
"repositories": [
{ "type": "composer", "url": "https://composer.typo3.org/" }
]
}
here is my controller
<?php
namespace GlobalizationPartners\Gpi\Controller;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
class gpiController extends ActionController
{
/**
* Default action for backend module
*
* #return object
* #since 5.2.0
*/
public function indexAction()
{
return GeneralUtility::makeInstance(\GlobalizationPartners\Gpi\Backend\gpiModule::class);
}
}

Package does not show on the front-end in Laravel

I try to create a package but for some reason, I cannot access the front-end.
This is my package structure, from the project root directory:
/package/contact/src/routes/web.php
/package/contact/composer.json
/package/contact/ContactServiceProvider
The service provider class looks like the following:
namespace Sidneylab\Contact;
use Illuminate\Support\ServiceProvider;
class ContactServiceProvider extends ServiceProvider
{
public function boot(){
$this->loadRoutesFrom(__DIR__.'/routes/web.php');
}
public function register(){
}
}
Composer.json
"name": "sidneylab/contact",
"description": "This will send email to admin and send contact query to database",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Sidney de Sousa",
"email": "esp.sousa#gmail.com"
}
],
"minimum-stability": "dev",
"require": {},
"autoload": {
"psr-4": {
"Sidneylab\\Contact\\": "src/"
}
},
web.php
Route::get('contact', function(){
return "contact";
});
In my main composer.json file I added this:
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/",
"Sidneylab\\Contact\\": "package/contact/src"
}
},
Also, I imported the service provider inside my confi/app.php
Sidneylab\Contact\ContactServiceProvider::class
Now, when I hit my url/contact it does not return anything. Anything I could have possibly missed?
Anything to do with the things I named?

How to add a package to a custom laravel package?

I'm building a custom Laravel package which requires the guzzlehttp/guzzle package. Below is my composer.json file:
{
"name": "lomse/awesomePackage",
"description": "this an awesome package",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Selom",
"email": "awesome#gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"guzzlehttp/guzzle": "^6.3"
},
"autoload": {
"psr-4": {
"Lomse\\AwesomePackage\\": "src/"
}
}
}
Below is the content of my AwesomeProvider.php file:
<?php
namespace Lomse\AwesomePackage;
use GuzzleHttp\Client;
use Illuminate\Support\ServiceProvider;
class AwesomeProvider extends ServiceProvider
{
public function boot(){
}
public function register()
{
$this->app->singleton(Awesome::class, function ($app) {
return new Awesome(new Client); //Class 'GuzzleHttp\Client' not found
});
}
}
I keep getting Class 'GuzzleHttp\Client' not found.
What am I doing wrong?
So, this turns out to be quite simple. I highlighted the steps to take in order to solve this. Hope this helps anyone who is having the same issue.
I had to push my code to a repo lomse/awesome-package on Github
then specified preferred-install as dist in the ./lomse/awesome-package/package.json config property`:
"config": {
"preferred-install": "dist"
}
The full code is
{
"name": "lomse/awesome-package",
"description": "this an awesome package",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Selom",
"email": "awesome#gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"guzzlehttp/guzzle": "^6.3"
},
"autoload": {
"psr-4": {
"Lomse\\AwesomePackage\\": "src/"
}
},
"config": {
"preferred-install": "dist"
}
}
In the root package.json, specify the repository of your package as follow:
"repositories": [
{
"type": "git",
"url": "git#github.com:lomse/awesome-package.git"
}
]
Also add your package repo to the package.json require property as shown below:
"lomse/awesome-package": "dev-master"
From your root directory, run the code below to update your dependencies. This will clone the lomse/awesome-package repo into your vendor folder and install any other dependencies required by your package:
composer update -vvv
-vvv is for debugging purposes

Resources