How to use my Package in Laravel5 - php

I have a question related to Package for Laravel 5. I am creating one package and tried to use that. package successfully created and using composer i can also get that in New Laravel setup , issue is that when i tried to use that it's says class not found. Here's my composer.json and Steps that i followed:
for e.g. my username = git_test and packagename = mypackage
My Package Structure :
**git_test > mypackage > src
My composer.json file
{
"name": "git_test/mypackage",
"description": "XXXXXXXXXX",
"keywords": ["laravel"],
"license": "MIT",
"authors": [
{
"name": "XXXXXXX",
"email": "XXXXXX#gmail.com"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "5.0.*"
},
"autoload": {
"psr-4": {
"git_test\\mypackage\\": "src/"
}
},
"minimum-stability": "dev"
}
Here's my src/myclass.php
namespace git_test\mypackage;
class myclass {
function test(){ echo "This is Test"; }
}
Now i am going to use this in my new laravel project so i add package in my directory composer and try to use the myclass in my HomeController
HomeController Code
use git_test\mypackage\myclass as TaskClass;
class HomeController extends Controller {
public function index()
{
$atTaskObj = new TaskClass('');
}
I got the error like "git_test\mypackage\myclass" Not Found. where i am doing wrong? any suggestion please.
Thanks in Advance!!!

PSR-4 paths have to end with \\:
"autoload": {
"psr-4": {
"git_test\\mypackage\\": "src/"
}
},

Related

Laravel 8 package development - Route Target class does not exist

I know the title topic sounds similar to other questions, but I've searched many topics on stackoverflow and none resolve my issue.
I am currently developing a package under Laravel ^8.12, below the content of the function that register my routes:
protected function registerRoutes(): void
{
Route::prefix('workflowmakr')
->namespace('AlvariumDigital\WorkflowMakr\Http\Controllers')
->as('workflowmakr.')
->middleware(config('workflowmakr.routes_middleware'))
->group(__DIR__ . '/../routes/api.php');
}
And below is the content of the routes/api.php file:
<?php
use Illuminate\Support\Facades\Route;
Route::resource('actions', 'ActionController')->except(['created', 'edit']);
Route::resource('scenarios', 'ScenarioController')->except(['created', 'edit']);
Route::resource('statuses', 'StatusController')->except(['created', 'edit']);
Route::resource('transitions', 'TransitionController')->except(['created', 'edit']);
For a better view of the project architecture, below is a screenshot of the packages folder containing the package under development:
And finally, below is the composer.json declaring my package:
...
"extra": {
"laravel": {
"providers": [
"AlvariumDigital\\WorkflowMakr\\WorkflowMakrServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/",
"AlvariumDigital\\WorkflowMakr\\": "packages/AlvariumDigital/WorkflowMakr/src/"
}
},
...
When I execute the command php artisan route:list to view all my routes I got this error :
$> php artisan route:list
Illuminate\Contracts\Container\BindingResolutionException
Target class [AlvariumDigital\WorkflowMakr\Http\Controllers\ActionController] does not exist.
at D:\Films\R_D\Laravel packages\workflow-makr\vendor\laravel\framework\src\Illuminate\Container\Container.php:832
828▕
829▕ try {
830▕ $reflector = new ReflectionClass($concrete);
831▕ } catch (ReflectionException $e) {
➜ 832▕ throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
833▕ }
834▕
835▕ // If the type is not instantiable, the developer is attempting to resolve
836▕ // an abstract type such as an Interface or Abstract Class and there is
1 [internal]:0
Illuminate\Foundation\Console\RouteListCommand::Illuminate\Foundation\Console\{closure}(Object(Illuminate\Routing\Route))
2 D:\Films\R_D\Laravel packages\workflow-makr\vendor\laravel\framework\src\Illuminate\Container\Container.php:830
ReflectionException::("Class AlvariumDigital\WorkflowMakr\Http\Controllers\ActionController does not exist")
EDIT
Below is the content of the ActionController file:
<?php
namespace AlvariumDigital\WorkflowMakr\Http\Controllers;
use AlvariumDigital\Models\Action;
use AlvariumDigital\WorkflowMakr\Helpers\Constants;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class ActionController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\JsonResponse
*/
public function index()
{
$query = Action::query();
if (config('workflowmakr.pagination_size') == -1) {
return response()->json($query->get(), 200);
}
return response()->json($query->paginate(config('workflowmakr.pagination_size')), 200);
}
// ...
}
EDIT 2
The content of the package composer.json file:
{
"name": "AlvariumDigital/WorkflowMakr",
"description": "Generalize the management of your workflows",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Alvarium Digital",
"email": "contact#alvariumdigital.com",
"homepage": "http://www.alvariumdigital.com",
"role": "Corporate"
},
{
"name": "EL OUFIR Hatim",
"email": "heloufir#alvariumdigital.com",
"homepage": "https://www.linkedin.com/in/eloufirhatim/",
"role": "Developer"
}
],
"support": {
"email": "heloufir#alvariumdigital.com"
},
"minimum-stability": "dev",
"require": {}
}
Did I do something wrong or incomplete?
You can ask for more details if needed.
Thanks
You have structured this directory incorrectly. You have the PSR4 autoloading loading the src directory as the namespace. Your controllers are not in the src folder, only the Service Provider is in there. So to composer there are no files for it to find and autoload based on your PSR4 autoloading.

PSR4 not working

I have setup a folder structure like this for a package of legacy classes
vendorname/legacy/src/ClassA.php
namespace Vendorname\Legacy;
class ClassA{}
vendorname/legacy/src/Folder/Class2.php
namespace Vendorname\Legacy\Folder;
class FolderClass2{}
With composer I'm loading this from a github repo like this:
"repositories": [
{
"type": "vcs",
"url": "git#bitbucket.org:username/vendorname-legacy-classes.git"
}
],
"require": {
"vendorname/legacy": "master#dev"
}
When I load ClassA like this it works:
use Vendorname\Legacy\ClassA;
$a = new ClassA();
However none of my subfolder'd classes work:
use Vendorname\Legacy\Folder\FolderClassB;
$b = new FolderClassB();
Class 'Vendorname\\Legacy\\Folder\\FolderClassB' not found
I have already defined the source folder with a file vendor\vendorname\composer.json
{
"name": "vendorname/legacy",
"description": "Vendorname Legacy classes",
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-4": {
"Vendorname\\Legacy\\": "src"
}
},
"extra": {
"branch-alias": {
"master": "master"
}
}
}
you need to define one thing more to your composer.json
{
"autoload": {
"psr-4": {"Vendorname\\Legacy\\": "vendorname/legacy/src/"}
}
}

Composer create local package

In my project inside the vendor directory I created a new directory named Hello and created a new class HelloWorld.php with the namespace Hello\Test. Then in the root of Hello I created a composer.json with default meta data (author, license, desc, required), and added autoload PSR-4 Hello\\Test\\.
So what do I need to do next to autoload my package. I looked at some Symfony components and their composer.json package and configuration is the same.
Is it possible to autoload my local package from vendor like this?
Dir structure:
|-my_project
|-composer.json
|---vendor
|-------composer
|-------autoload.php
|-------Hello
|-----------composer.json
|-----------HelloWorld.php
./vendor/Hello/composer.json
{
"name": "Zend/Hello",
"description": "My test package",
"license": "MIT",
"authors": [
{
"name": "Zend Zend",
"email": "test#example.com"
}
],
"autoload": {
"psr-4": {
"Hello\\Test\\": ""
}
}
}
My HelloWorld.php class has namespace Hello\Test;
Inside index.php i do include 'vendor/autoload.php
And root composer.json
{
"autoload": {
"psr-4": {
"": "src/",
"Hello\\Test\\": "./vendor/Hello"
}
},
"require": {
"Hello/Test": "*"
}
}
composer update
Okay! Sorry for the late reply.
You only need one composer.json to make this work. The one in your main project directory.
Try using this:
{
"name": "Zend/Hello",
"description": "My test package",
"license": "MIT",
"authors": [
{
"name": "Zend Zend",
"email": "test#example.com"
}
],
"autoload": {
"psr-4": {
"Hello\\Test\\": "vendor/Hello"
}
}
}
And your HelloWorld.php file should be like:
<?php
namespace Hello\Test;
class HelloWorld {
// your methods goes here
}
Run a composer self update to get the latest composer the run composer update and it should now be added to your vendor/composer/autoload_psr4.php file

Composer for autoloading php

Hi i have this folder structure:
I'm using composer for autoloading my files but it is dont work .. i do it first time and i dont know how to implement this.
My composer.json
{
"name": "Some name",
"description": "Some Framework",
"minimum-stability": "stable",
"license": "proprietary",
"authors": [
{
"name": "Some names of authors",
"email": "some#gmail.com"
}
],
"autoload": {
"psr-4": {
"Apison": "/../sdk/"
}
}
}
And my index.php
<?php
require_once 'vendor/autoload.php';
$app = new \Apison\Sdk\App();
When i update my composer it will write: Nothing to load and PHP will catch exeption on line with $app = new \Apison\Sdk\App();
Thanks for your tips
namespaces need \\:
"Apison\\": "../sdk"
documentation:
https://getcomposer.org/doc/04-schema.md#psr-4
Based on our chat, the solution is this:
"autoload": {
"psr-4": {
"Apison\\Sdk\\": "sdk"
}
}
Then the namespaces and file structure was changed to comply with the psr-4 standard

Referencing a custom composer package in Slim PHP

I'm trying to figure out how to reference a custom class using composer
my composer.json file looks like this:
{
"name": "adtools_api",
"repositories": [
{
"type": "package",
"package": {
"name": "qz/adtools_middleware",
"version": "dev-master",
"source": {
"url": "repo-name",
"type": "git",
"reference": "master"
}
}
}
],
"require": {
"slim/slim": "2.*",
"qz/adtools_middleware": "src/"
}
}
and the folder structure looks like this:
app
routes
vendor
composer
qz
adtools_middleware
src
hello-world.php
slim
composer.json
index.php
I'm trying to reference the hello-world.php file which looks like this:
<?php
namespace HelloWorld;
class SayHello
{
public static function world()
{
return 'Hello World, Composer!';
}
}
?>
In the index.php file I'm trying to reference the class like this:
$hello = new HelloWorld\SayHello();
but getting an error telling me "Fatal error: Class 'HelloWorld\SayHello' not found in..."
If anyone can point me in the right direction that would be great! Thank you!
Can you check the autoload inside the vendor folder and see if your HelloWorld namespace is loaded?
If not, you may need to add autoload attribute to your composer.json file, like this
{
"autoload": {
"psr-0": {"HelloWorld": "qz/adtools_middleware/src/"}
},
to load the HelloWorld namespace in your project

Categories