Making Laravel Package. but "Class Not Found" - php

I'm making laravel's package.
But Class "Username/PackageName/Class" not found.
I read this.
But I can't find problem.
Please help me.
This is my codes
composer.json
{
"name": "username/packageName",
"require": {
"php": "7.3",
"laravel/framework": "^6.0"
},
"autoload": {
"psr-4": {
"username\\packageName\\": "src/"
}
},
"license": "MIT",
"authors": [
],
"extra": {
"laravel": {
"providers": [
"username\\packgeName\\TestServiceProvider"
]
}
}
}
src/TestServiceProvider
<?php
namespace username\packageName;
use Illuminate\Support\ServiceProvider;
class TestServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
$this->app->bind('test', function ($app) {
return new Test();
});
}
}
src/Test.php
<?php
namespace username\packageName;
class Test {
public static function test(){
return 'abc';
}
}
in Controller
use username\packageName\Test;
/* ~~~~ */
dd(Test::test()); // error
after add current composer.json.
"repositories": [
{
"type": "path",
"url": "vendor/username/packageName",
"symlink": true
}
]
> composer dump-autoload
Thank you for reading my codes.
username -> my username
packageName -> my package name

Have you extend Facades class in your Test Class.
If you want to fetch all function of your files statically,
class Test extends Facades {
public static function getFacadeAccessor(){
return 'classNameWhereFunctionIsWritten' //say TestService has test() function
}
}
And Pass this classNameWhereFunctionIsWritten in your provider 'classNameWhereFunctionIsWritten' here.

Related

Interface "Composer\Plugin\PluginInterface" not found

I am writing composer package and in my plugin code I am trying to get Composer\Plugin\PluginInterface but I'm receiving Interface "Composer\Plugin\PluginInterface" not found when I install my package.
My plugin composer.json
{
"name": "..../.....",
"description": "....",
"keywords": [
//...
],
"license": "MIT",
"authors": [
{
"name": "...",
"email": "...",
"homepage": "...",
}
],
"type": "composer-plugin",
"require": {
"php": ">=8.0",
"composer-plugin-api": "~2.0", <-- here
},
"autoload": {
"psr-4": {
"...\\....\\": "src/"
}
},
"extra": {
"class": "...\\....\\....",
"laravel": {
"providers": [
"...\\....\\...."
]
}
}
}
And here is my plugin structure: (In case I forget to add something!)
<?php
namespace ...\....;
use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface; <-- Not Found!
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
class MyClassPlugin implements PluginInterface, EventSubscriberInterface
{
public static function getSubscribedEvents(){}
public static function update(){...}
public static function myFunctionOne(){...}
public static function myFunctionTwo(){...}
public static function myFunctionThree(){...}
public static function generateConfig(){...}
public static function activate(){...}
public static function deactivate(){...}
public static function uninstall(){...}
}
Also this is my installed composer: Composer version 2.3.5
Any idea?

PHP Class not found using namespaces

I can't find out why my classes won't load. I am using Composer for psr-4 autoloading and have been using it successfully. Here's how I have my classes setup:
/project
/classes
/feeds
/pull
/factory
composer.json
testMyFactory.php
feeds/factory/FeedFactory.php
namespace MyClasses\Feeds\Factory;
interface FeedFactory
{
public function build($provider);
}
feeds/factory/PullFeedFactory.php
namespace MyClasses\Feeds\Factory;
use MyClasses\Feeds\Factory\FeedFactory;
use MyClasses\Feeds\Pull\Providers\One;
/**
* Class FeedFactory
*/
class PullFeedFactory implements FeedFactory
{
public function __construct(){}
/**
* Build provider object for factory
* #param string $provider Type of feed provider to return
* #return Object Provider object
*/
public function build($provider) {
switch ($provider) {
case 'one':
$provider = new One();
break;
default:
$provider = new One`();
break;
}
return $provider;
}
}
project/feeds/pull/One.php
namespace MyClasses\Feeds\Pull\Providers;
class One
{
public function pull() {
echo 'Pull One';
}
}
project/testMyFactory.php
require __DIR__ . '/vendor/autoload.php';
use MyClasses\Feeds\Factory\PullFeedFactory;
$feed = new PullFeedFactory();
$feed->build('one');
$feed->pull();
project/composer.json
{
"require": {
//Remove for example
},
"config": {
"preferred-install": "dist"
},
"require-dev": {
},
"autoload": {
"psr-4": {
"MyClasses\\": "./classes",
}
}
}
This is the error I keep getting Class 'MyClasses\Feeds\Factory\PullFeedFactory' not found in /var/www/html/testPullFactory.php on line xx
I have other classes that work in the Classes directory with autoload but for some reason cannot get this to work. I feel like it's something glaringly obvious but have been stuck on this for hours now.
UPDATE:
Updated to include my vendor/autoload.php file. Stil getting an error, although it's different now Class 'MyClasses\Feeds\Pull\Providers\One' not found in /var/www/html/classes/Feeds/Factory/PullFeedFactory.php
in your composer.json, change to
"autoload": {
"psr-4": {
"MyClasses\\": "classes/",
}
}
Then, execute composer dump-autoload.

Composer - unable to autoload files

I have a simple composer project on GitHub with the directory structure as:
/--
-composer.json
-lib/ComposerTest.php
with the ComposerTest.php file as:
namespace lib;
class ComposerTest{
public function doTest(){
return "This class was loaded from Composer\n";
}
}
and composer.json as:
{
"name":"sc/composerTest",
"autoload":{
"psr-0":{
"lib":"./"
}
}
}
Using the following composer.json file, I'm able to include the GitHub project into my vendor folder, but cannot get it to autoload.
{
"description" : "The CodeIgniter Application with Composer",
"require": {
"php": ">=5.3.2",
"codeigniter/framework": "3.1.*",
"kriswallsmith/buzz":"*",
"maltyxx/bower": "^1.0",
"sc/composerTest":"dev-master"
},
"require-dev": {
"mikey179/vfsStream": "1.1.*"
},
"repositories": [
{
"type": "git",
"url": "https://github.com/sc/composerTest.git"
}
]
}
Can someone advise?
Edit: ComposerTest Controller
class ComposerTest extends CI_Controller
{
public function index()
{
$composerTest = new ComposerTest();
echo $composerTest->doTest();
}
}

Laravel PHP: Repository Class does not exist

I'm still new with using Repositories in Laravel PHP. I have created a repository file and have placed it in a directory called 'Repositories' witin my project's root folder. In my homepage's controller, I have created a '__construct()' function for the repository but when I try to access this page, I keep getting a 'Class Repositories\VideoRepository does not exist' error.
I'm not quite sure why I keep getting this error even after I use 'php artisan dump-autoload'. Am I not putting the repository in the right folder?
Controller(app\OverviewController.php):
<?php
use Controllers\VideosController;
use Models\Video;
use Models\Validators as Validators;
class OverviewController extends BaseController {
/* The Video model */
protected $video;
/* The picture model */
protected $picture;
/* The layout for the Videos and Pictures Overview */
protected $layout = 'layouts.master';
public function __construct()
{
$this->video = \App::make('Repositories\VideoRepository');
}
/* List all the videos and stones
Included Pagination for neatness */
public function index()
{
$allpicsvids = Video::paginate(10);
$this->layout->content = \View::make('overview', array('allpicsvids' => $allpicsvids));
}
}
Repository(app\repositories\VideoRepository.php):
EDIT: Added the namespace 'app\repositories' to this interface.
<?php namespace app\repositories;
interface VideoRepository {
public function all();
public function find($id);
public function findOrFail($id);
public function create($input);
public function update($id, $input);
public function delete($id);
public function forceDelete($id);
public function restore($id);
}
Eloquent Repository(app\repositories\EloquentVideoRepository.php):
<?php namespace Repositories;
use Models\Video;
class EloquentVideoRepository implements VideoRepository {
public function all()
{
return Video::all();
}
public function find($id)
{
return Video::find($id);
}
public function findOrFail($id)
{
return Video::findOrFail($id);
}
public function create($input)
{
return Video::create($input);
}
public function update($id, $input)
{
$video = Video::find($id);
$video->video_name = $input['video_name'];
$video->video_description = $input['video_name'];
$video->video_edges = $input['video_edges'];
$video->video_stores = $input['video_stores'];
$video->video_order = $input['video_order'];
$video->video_link = $input["video_link"];
$video->video_height = $input['video_height'];
$video->video_width = $input['video_width'];
$video->category = $input['category'];
$video->video_project = $input['video_project'];
$video->touch();
return $video->save();
}
public function delete($id)
{
$video = Video::find($id);
return $video->delete();
}
public function forceDelete($id)
{
$video = Video::find($id);
return $video->forceDelete();
}
public function restore($id)
{
$video = Video::withTrashed()->find($id);
return $album->restore();
}
}
composer.json:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"laravel/framework": "4.2.*"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
/* Added this line below so that my repositories could be recognized */
"app/repositories",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "stable"
}
EDIT: After adding a "psr-4" block of code for my repositories and php artisan dump-autoload, this is what my 'autoload_psr4.php' currently looks like:
<?php
// autoload_psr4.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'repositories\\' => array($baseDir . '/app/repositories'),
'Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'),
);
You could load the repositories via psr-4 instead of trying to add it to the class map:
In your composer json:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
psr-4: {
"Repositories\\" : "Repositories"
}
},
You would also need to add your namespace in the top of the VideoRepository file.
Also, I would suggest you move your Repositories directory into your app folder. This way, your custom code will all reside in the framework's folder that's meant to contain your additions. The composer.json psr-4 would look like this then:
"Repositories\\" : "app\\Repositories"
And your namespace in any of the Repository files would be:
<?php namespace app\Repositories;
I would also suggest checking out the Laracast video on PSR-4 autoloading. It's super helpful and if you plan on building stuff in laravel it's well worth the money.
Your interface isn't in any specific namespace. Your EloquentVideoRepository is - hence the class can't be found as they sit in the same directory.
Easy enough to fix if you pull your interface into the same namespace as the implementing class.
Also, the psr-4 autoloading should be "Repositories\" : 'repositories' unless you want to adjust the casing on the folder name.
Sounds like it's not being autoloaded, you need to add it to composer.json.
You can add it to the autoload/classmap array or take a look at using PSR autoloading.

ServiceProvider not found on phpunit testing for Laravel 4

I am new to laravel 4, and I am following a Laravel tutorial on Culttt.com right now. I added a package into the project and create a Facade to access: Philipbrown/Suypo, it works fine.
workbench\philipbrown\supyo\src\Philipbrown\Supyo\SuypoServiceProvider.phh
<?php namespace Philipbrown\Supyo;
use Illuminate\Support\ServiceProvider;
class SupyoServiceProvider extends ServiceProvider {
protected $defer = false;
public function boot()
{
$this->package('philipbrown/supyo');
}
public function register()
{
$this->app['supyo'] = $this->app->share(function($app)
{
return new Supyo;
});
$this->app->booting(function()
{
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Supyo', 'Philipbrown\Supyo\Facades\Supyo');
});
}
public function provides()
{
return array('supyo');
}
}
This is the composer.json file of my package:
{
"name": "philipbrown/supyo",
"description": "",
"authors": [
{
"name": "ChaoMeng",
"email": "cmeng#idfbins.com"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "4.2.*"
},
"autoload": {
"classmap": [
"src/migrations"
],
"psr-0": {
"Philipbrown\\Supyo": "src/"
}
},
"minimum-stability": "stable"
}
But when I write some tests and use phpunit to run them, it shows this error:
Fatal error: Class 'Philipbrown\Supyo\SupyoServiceProvider' not found in C:\Dev\wamp\www\Culttt\laravel\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php on line 158
I tried to run command: composer dump-autoload but it does not work. and I did not call or use this package in the test, so I really don't know what happens here, below is my test.php:
class CliqueTest extends TestCase {
/**
* Test that the name is required for Clique
*/
public function testNameIsRequired()
{
// Create a new Clique
$clique = new Clique;
// Post should not save
$this->assertFalse($clique->save());
// Save the errors
$errors = $clique->errors()->all();
// There should be 1 error
$this->assertCount(1, $errors);
// The error should be set
$this->assertEquals($errors[0], "The name field is required.");
}
public function testCliqueUserRelationship()
{
// Create a new Clique
$clique = FactoryMuff::create('Clique');
// Create two Users
$user1 = FactoryMuff::create('User');
$user2 = FactoryMuff::create('User');
// Save Users to the Clique
$clique->users()->save($user1);
$clique->users()->save($user2);
// Count number of Users
$this->assertCount(2, $clique->users);
}
}
So please give me a idea about what's going on. Thanks in advance.
This is the whole code in github: https://github.com/mc422/laravel.git

Categories