How to update composer.json from Laravel controller - php

So I'm building a Laravel package and there is a special features that requires updating composer by adding a psr-4 namespace which points to a directory in Laravel base path.
I have tried this so far but doesn't work.
$loader = include(base_path('vendor/autoload.php'));
$loader->add('Classes\Weather', base_path('modules'));
Later:
$weather = new Classes\Weather\WeatherSite();

You might check this thread, which gives some solutions:
https://github.com/composer/composer/issues/1906#issuecomment-51632453

After several trials and going through Composer documentation, I was able to come up with this which works:
NB: The reason why I needed this solution is to enable me add a Psr4 path automatically from a Laravel package without manually adding a specific path required in the package manually in composer.json
Add this in the boot method of your package service provider , mine is DigitlimitModuleServiceProvider
use Illuminate\Support\ServiceProvider;
class DigitlimitModuleServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
$autoload = require base_path('vendor/autoload.php');
$autoload->addPsr4('Digitlimit\\Module\\', base_path('modules'));
$autoload->register();
if(!file_exists(base_path('modules'))){
\File::makeDirectory(base_path('modules'));
//works as long as there is permission
}
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
}
}

Related

Laravel CLI Custom Package without Registering Manual to config/app.php

I just created a simple Laravel CLI package in Laravel 8. My custom command doesn't appear in php artisan list and the provider needs to register manually into config/app.php to make it work. As far as I know, we don't need to register the provider manually in Laravel 5.5+.
Here is my provider source code:
<?php
namespace Robyfirnandoyusuf\BadOmen;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\ServiceProvider;
use Robyfirnandoyusuf\BadOmen\Commands\Migrate;
class BadOmenServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
Migrate::class
]);
}
}
public function register()
{
}
}
and this is the structure of the directories:
Anyone have solution for this problem?
Solved, by adding providers property in the package's composer.json file, with a value of [Robyfirnandoyusuf\BadOmen\BadOmenServiceProvider::class]

use of middleware in custom package devlopment in laravel

I want to build packages in which I want to make middleware which validate data from different services(different project) by just installing that package.
what is a step to making this type of command and publish in any project?
I read the document. I understand that how to make view(loadViewFrom), route(loadRouteFrom) but didn't find any methods for middleware like loadViewFrom, loadRouteFrom. and how to publish that packages.
In your service provider file you add a variation of the following:
use Illuminate\Routing\Router;
use yourpackageauthor\YourPackageName\App\Http\Middleware\YourMiddlwareClass;
...
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot(Router $router)
{
$router->middlewareGroup('yourMiddlwareName', [YourMiddlewareClass::class]);
}
For reference see:
https://github.com/jeremykenedy/laravel-blocker/blob/master/src/LaravelBlockerServiceProvider.php

How to use core php library in laravel?

. I want to implement this package in my laravel. Package link is below:-
https://github.com/joshdick/miniProxy/blob/master/miniProxy.php
Its working fine when i run using php file. But i don't knw how to implement this package file in laravel. This package contains only one file. Can anyone guide me or help me how to do this package functioning in laravel.
You can simply include this file in your class. Put it somewhere meaningful, like /vendor or /lib and include it in the class where you want to use it.
Some information on including external PHP files: https://laraveldaily.com/how-to-use-external-classes-and-php-files-in-laravel-controller/
simply add it in your composer.json
You can create a ServiceProvider like this in app/Providers :
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename) {
require_once($filename);
}
}
}
In you config/app file, add this new serviceProvier
App\Providers\HelperServiceProvider::class,
Then create a folder /Helpers in /app folder (./app/Helpers) and put your file in this folder.
Now you can access all this folder functions from everywhere.

Laravel 5.4 and WordPress 4.7 conflict in helper function __()

I am developing a website using Laravel, with a WordPress Blog section running in parallel and independently.
Since Laravel 5.3 everything run smoothly, using a Service Provider I was able to include 'wp-load.php' with a require_once() call and use all WordPress function out of the box and get/update WordPress posts.
Unfortunately, in Laravel 5.4 the helper function __() has been defined and this generates a conflict with the same WP function declared in l10n.php.
I tried using namespaces but with no luck.
This is the code of my Service Provider:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class WordPressServiceProvider extends ServiceProvider
{
/**
* Path to our WP installation
*
* #var string
*/
protected $bootstrapFilePath = '/wp-paths/wp-it-news/wp-load.php';
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
if (\File::exists(public_path() . $this->bootstrapFilePath))
{
require_once(public_path() . $this->bootstrapFilePath);
}
}
}
You can include wp-load.php before vendor/autoload.php in bootstrap/autoload.php
I ended up using WordPress API, as Cbroe suggested.
Docs here
curl -X OPTIONS -i http://demo.wp-api.org/wp-json/wp/v2/posts

Laravel 4 workbench class not found

I'm trying to develop a package in laravel 4 - my first attempt at a package.
I found a couple of tutorials which I've tried to follow:
http://jasonlewis.me/article/laravel-4-develop-packages-using-the-workbench
and
http://culttt.com/2013/06/24/creating-a-laravel-4-package/
and of course in the official documentation.
I've followed the basic structure to create the framework. However on loading the app I get a class not found error. This relates directly to the serviceprovider I have placed in the app.php file.
here's my entry in the providers array:
'Longestdrive\Calendar\CalendarServiceProvider'
My folder structure is:
laravel/workbench/longestdrive/calendar/src/Longestdrive/Calendar
My service provider has the following entries:
<?php namespace Longestdrive\Calendar;
use Illuminate\Support\ServiceProvider;
class CalendarServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* #var bool
*/
protected $defer = false;
/**
* Bootstrap the application events.
*
* #return void
*/
public function boot()
{
$this->package('longestdrive/calendar');
}
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
//
}
/**
* Get the services provided by the provider.
*
* #return array
*/
public function provides()
{
return array();
}
}
I've double checked to spelling and ran a composer dump-autoload both from the root of the project and the root of the package.
I've run out of ideas for solving the class not found any ideas where I've gone wrong?
The line producing the error is this one:
C:\wamp\www\googleapi\laravel\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php
Any help appreciated
Thanks
Update:
I ran a composer update as suggested in the workbench/package folder with a response nothing to update. I then ran composer at the root of the project and an error was produced:
[RuntimeException]
Error Output: PHP Fatal error: Class 'Longestdrive\Calendar\CalendarServiceProvider' not found
in C:\wamp\www\googleapi\laravel\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRe
pository.php on line 123
I probably posted the wrong error line earlier. The full exception response is:
Class 'Longestdrive\Calendar\CalendarServiceProvider' not found
THe error extract:
* #param \Illuminate\Foundation\Application $app
* #param string $provider
* #return \Illuminate\Support\ServiceProvider
*/
public function createProvider(Application $app, $provider)
{
return new $provider($app);
}
which I assume relates to the service provider loader not finding the CalendarServiceProvider?
I found that running composer install from within the workbench/[vendor]/[package] folder solved the problem.
I encountered the same error, so I went deeper on its flow to knew what happens.
So dissecting a little bit basically, in the bootstrap phase, when bootstrap/autoload.php is loaded it runs at the end:
if (is_dir($workbench = __DIR__.'/../workbench'))
{
Illuminate\Workbench\Starter::start($workbench);
}
This requires EVERY workbench/vendor/package/**/**/**/autoload.php he found (by using Symfony Finder Component)
$finder->in($path)->files()->name('autoload.php')->depth('<= 3');
That's important because it's expecting to find workbench/vendor/package/vendor/autoload.php.
Successively in bootstrap/start.php it gets the 'providers' defined in config/app.php and try to load each of them:
$providers = $config['providers'];
$app->getProviderRepository()->load($app, $providers);
and then in ProviderRepository.php
foreach ($providers as $provider)
{
$instance = $this->createProvider($app, $provider);
so we'll end up with:
public function createProvider(Application $app, $provider)
{
return new $provider($app);
where it tried to create an instance of a class isn't really autoloaded. And so that's why the exception thrown!
In conclusion...
As #Ray said, by removing his Service from 'providers' => array( no error is thrown cause return new $myServiceDeclaredInProviderArray($app); never fires for that service.
As #Andrew Holt said
I found that running composer install from within the workbench/[vendor]/[package] folder solved the problem.
He's absolutely right because this create the autoload vendor dir and files, and everything works as we expect it to because it finds the autoload files:
$finder->in($path)->files()->name('autoload.php')->depth('<= 3');
Me
php artisan dump-autoload works as well if you remove the service from the providers array
In addition to #ilpaijin's and #Andrew Holt's answer, there sometimes comes the need (when there's a new version of Laravel) to run composer update within the workbench/vendor/package folder.
Also, as noted here, the composer.json within the package must require the same version of illuminate/support as the one required of laravel/framework in the project root's composer.json.
Thanks to #biscii note that one should use:
"illuminate/support": "4.1.x"
instead of
"illuminate/support": "4.x"

Categories