Manually attach package to Laravel (unable to use composer due to firewall) - php

I can't use composer to handle my dependencies due to the corporate firewall. At the moment I'm trying to use Barry vd Heuvel's DomPDF wrapper for Laravel and tried to:
Download the zipfile from Github (master)
Updated composer.json (not sure if its needed, but did it anyway) and added "barryvdh/laravel-dompdf": "*" in the require container.
Create the folder structure: vendor/barryvdh/laravel-dompdf
Place all files from the package in there (config-folder, src-folder and the files .gitignore, composer.json and readme.md)
Add the service provider and facade in my app.php. Service provider is listed as Barryvdh\DomPDF\ServiceProvider::class and the facade is aliased like 'PDF' => Barryvdh\DomPDF\Facade::class
Ran composer dump-autoload
After refreshing the browser I'm getting Class 'Barryvdh\DomPDF\ServiceProvider' not found. I also tried to run php artisan cache:clear and php artisan dump-autoload but the last one fails over the fact it can't find Barryvdh\DomPDF\ServiceProvider.
What have I forgotten to do to make it work?
Update
I've tried the suggested answer from Wouter J and the composer.json now looks like:
..
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Barryvdh\\DomPDF\\": "vendor/barryvdh/laravel-dompdf/src"
},
..
I've verified if the composer dump-autoload had any effect but I think it had. Because the entry is now also listed in vendor/composer/autoload_psr4.php like:
return array(
// more entries
'Barryvdh\\DomPDF\\' => array($vendorDir . '/barryvdh/laravel-dompdf/src'),
'App\\' => array($baseDir . '/app'),
);
I believe at this point this is working, but the Facade isn't responding. When I try to call something like PDF::loadView(...) and I let PhpStorm import the class (vendor/barryvdh/laravel-dompdf/src/PDF.php) it throws an error I can't call the method loadView statically. According to the documentation I should be able to call it like this:
$pdf = PDF::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');
But that results in Non-static method Barryvdh\DomPDF\PDF::loadView() should not be called statically, assuming $this from incompatible context on my end.
Suggestions?

Composer autoload still doesn't know anything about how to download the package. You have to configure autoloading like this:
{
"autoload": {
"psr-4": { "Namespace\\Of\\The\\Package\\": "vendor/the/package" }
}
}

I know this question is old, but since I don't see any solution, here is mine: I had exactly the same problem. Was able to solve by running composer dumpautoload on my localhost and then copied the folder 'vendor/composer' to the shared hosting without an SSH access. Hope this will help someone in similar situation.

The problem addresses that application don't know about DomPDF\ServiceProvidor or anything related to DomPDF because barryvdh/laravel-dompdf itself depends dpmpdf/dompdf package which is still missing and does not comes with barryvdh/laravel-dompdf
the package dompdf/dompdf also depends on several extension i.e.
PHP version 5.3.0 or higher
DOM extension
GD extension
MBString extension
php-font-lib
php-svg-lib
have a look at package here https://packagist.org/packages/dompdf/dompdf download this and put in your vendor directory then update your composer dumpautoload it should be working

Related

How can I check if PHP Composer autoload is working?

I am trying to transfer a small web application, which uses PHP Composer, from an old out of date server to a new server.
On the new server, I have run composer install to install the dependencies from the composer.lock file.
The web application requires a config file to set itself up (not that it does a lot of setting up), which includes the line
require_once __DIR__ . '/vendor/autoload.php';
…which should locate the autoloader relative to the config file, and make the packages installed by Composer known to the system.
However, this doesn't seem to be working properly, as when the web app tries to use SwiftMailer (which was installed via Composer), it says:
PHP Fatal error: Uncaught Error: Call to undefined method Swift_SmtpTransport::newInstance()
…in the file that calls it, which obviously suggests that it does not know about it and cannot find it.
Is there a way that I can meaningfully check what the result of the autoloader include is and see what it is (or is not) doing?
Make sure to add a use statement to your classes. You can arrange your classes and give your classes namespaces then allow composer to also auto-load your classes.
You can tell composer how your classes are arranged according to namespaces as follows:
In the autoload section of your composer.json file edit it to know how the namespaces of your classes is arranged
"autoload": {
"psr-4": {
"App\\": "src/",
"Mynamespace\\": "lib/src/ProjectSrc/"
}
},
The autoload section now tells composer the Classes in the ProjectSrc folder will have a root namespace of Mynamespace. From this information composer can determine all other sub namespaces in your directory tree. This will not happen automatically composer has to rebuild its mapping of classes with the composer dump-autoload command.

Composer complains about non-compliance to PSR-4, even though everything seems fine

In my project, I'm including a package I'm developing, which has a composer.json, which includes the following autoload entry:
(censored to not get in trouble with my company of course)
{
"autoload": {
"psr-4": {
"Vendor\\Package\\": "src/Package"
}
}
}
The main project and the package both have a composer.json, this is from the package's composer.json
I have also seen some examples that only used "Vendor\\", but this resulted in the exact same issue.
I am requiring the package on version tag (from a git repository), so it should get and install the package correctly.
Any time composer runs autoload, it complains the namespaces don't conform to PSR-4. I have checked the capitalisation, which all checks out. It complains about pretty much every php file in my project. Here's an example of the file structure:
vendor
|- package
|- src
|- Package
|- PackageServiceProvider.php
The namespace of PackageServiceProvider.php is Vendor\Package, and the class is PackageServiceProvider.
As far as I know, this is how it's supposed to be. Yet composer still gives the deprecation notice.
I should probably mention that I have also tried running composer clearcache between all of the things I've tried (which are mainly changing capitalisation), which didn't help.
I am completely out of ideas on how to fix this.
I'm on composer version 1.10.13, and Laravel version 5.8.38.
One of the many deprecation notices I'm getting:
Deprecation Notice: Class Vendor\Package\PackageServiceProvider located in D:/wwwroot/project/vendor/{package-vendor}/package/src/Package\PackageServiceProvider.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar://C:/Users/me/AppData/Local/ComposerSetup/bin/composer.phar/src/Composer/Autoload/ClassMapGenerator.php:201
As it turns out, the version of the package that's on our git repository, had a mistake in its composer.json which I'd long fixed locally. But since it wasn't fixed on the repo, composer had never finished updating the dependencies, and hadn't updated its links to the packages.
All I had to do was change
{
"autoload": {
"psr-4": {
"Vendor\\": "src/Package"
},
}
}
to
{
"autoload": {
"psr-4": {
"Vendor\\Package\\": "src/Package"
},
}
}
and push it to the repository.
That's wrong approach. That package, which apparently you manually planted in your vendor/ folder (which is, let's say, moderately OK) should be valid composer package with own composer.json file. Then ordinary composer dumpautoload would perfectly suffice to build class map for autoloading. No oddities like yours needed.

Laravel, how to manually install package without composer

Wanted to install Laravel-Excel (Maatwebsite) package manually without composer, but I dont know how.
Why? Because I have a laravel project in a free hosting server setup by other guy, and I can only access using Filezilla to edit/download/upload the codes.
If only Filezilla allow a command prompt that could use "composer update", then it will be easier.
I got solution!
I cant use composer on my company because of secure network. But i can download zip form github and install it manual. The below is my example for HTMLPurifier:
download and extract library mews/purifier to vendor directory https://github.com/mewebstudio/Purifier
add below line in vendor/composer/autoload_psr4.php
This sentence will load all of file from vendor/mews/purifier/src and autoload in namespace Mews\Purifier\
'Mews\\Purifier\\' => array($vendorDir . '/mews/purifier/src'),
Sometime you need add library into autoload_namespaces.php intead of, please read in
https://getcomposer.org/doc/04-schema.md#autoload
You got Mews\Purifier\Facades\Purifier not found if public config before finish step 3
$ php artisan vendor:publish
--provider="Mews\Purifier\PurifierServiceProvider"
add below json in vendor/composer/installed.json
This for composer history, providers and aliases will be load in config/app/php for register new provider
{
"name": "mews/purifier",
"version": "v2.0.12",
"type": "library",
"extra": {
"laravel": {
"providers": [
"Mews\\Purifier\\PurifierServiceProvider"
],
"aliases": {
"Purifier": "Mews\\Purifier\\Facades\\Purifier"
}
}
},
"autoload": {
"psr-4": {
"Mews\\Purifier\\": "src/"
}
}
},
Now you run this config, then vendor/mews/purifier/config will be move to config folder
$ php artisan vendor:publish
--provider="Mews\Purifier\PurifierServiceProvider"
Add the package to the vendor folder. You can upload it using filezilla
Add a reference in \vendor\composer\autoload_namespaces.php
Add a reference in \vendor\composer\autoload_psr4.php
Source laravel.io
it's easy to do it by following this
download the package and set the files in app folder
YourProject/app/Laravel-Excel/
and then add the path to composer.json in autoload
"autoload": {
...
"classmap": [
"database/seeds",
"database/factories"
"app/Laravel-Excel"
],
...
},
Run the composer dump-autoload
the solution refs to this question referance answer
download the package locally and then upload the package folder (found under vendor) along with the updated composer.json
Depending on how strict the server is, you could SSH into your Server. But doing it locally then uploading the required files is usually the way to go.
You might need to run composer autodump if you don't wipe the cache.
If everything works on local environment then copy your package and composer folder to server which is located at vendor
upload \vendor\maatwebsite
copy \vendor\maatwebsite\excel\src\config\excel.php to \config\excel.php

ServiceProvider not found Laravel 5.0

I'm trying to create a custom package for Laravel 5.0 based on this tutorials
The folder structure and service providers are exactly same, but some how the Serviceprovider is not updating autoload_namespace.php.
I already added my service provider in app/config.php
'Walkswithme\Users\UsersServiceProvider',
In my root composer.json I have following code.
"psr-4": {
"App\\": "app/",
"Walkswithme\\Users\\": "packages/walkswithme/users/src"
}
Under my packages folder files struture is as follows.
walkswithme
users
src
models
controllers
views
UsersServiceProvider.php
routes.php
composer.json
I can't use Laravel 5.1 it requires php 5.5.9 , Other wise I can user artisan packager command.
The error is getting is as follows.
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'Walkswithme\Users\UsersServiceProvider' not found
Any help will be appreciated last 3hrs I'm digging on it.
I solved it myself ,
I tried composer dumpautoload -o so it works for me.
Also some time needs composer clearcache too.
Hope it helps someone else..
For me it was
Class 'Jubeki\LaravelCodeStyle\ServiceProvider' not found
And I just run
composer require jubeki/laravel-code-style --dev
In another folder (not where my common composer.json is located)
Maybe it also can help someone :)
Just check the place of the new bundle installation folder.

Httpful and Laravel, error Class 'Httpful' not found

I've installed Httpful as described with Composer adding to composer.json the following:
{
"require": {
"nategood/httpful": "*"
}
}
I'm using Laravel 4 so i ran composer install
I've checked if the plugin is installed and is there, in fact under the vendor folder of laravel i can find it. But i keep getting the following error:
ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class 'Httpful' not found'
I'm missing some steps?
Thank you in advance
The class related to this package name conflicts with Laravel's Response class, so this is how you use it in Laravel:
$url = "http://api.openweathermap.org/data/2.5/weather?lat=22.569719&lon=88.36972";
$response = \Httpful\Request::get($url)->send();
echo $response->body->name."<br>";
echo $response->body->weather[0]->description;
The class is not Httpful, but Response, so you have to add the correct Namespace so it doesn't get confused by Laravel's Response class.
EDIT:
In Laravel you can create aliases for classes. Edit your app/config/app.php and in the aliases array, add:
'aliases' => array(
....
'Httpful' => '\Httpful\Request',
),
And you'll be able to use it this way:
$response = Httpful::get($url)->send();
You may need to do composer update rather than composer install.
The difference being install will go by whatever is in your composer.lock file, while update will pick up any new dependencies added into your composer.json file, and then write those to your composer.lock file.
Note: install should generally be used in production to get the latest from your composer.lock file, while update is generally a command used in development to get your updated dependencies.
This also means you should add composer.lock to your git repository, even tho it's in your .gitignore file by default in a new Laravel project.

Categories