Laravel PHP: Repository Class does not exist - php

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.

Related

Making Laravel Package. but "Class Not Found"

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.

Creating composer package for Codeigniter 4

I am trying to create my first composer package for Codeigniter 4. However, I always get an error Class 'Myapp\Settings\Greet' not found. I'm totally lost.
I created a folder inside the ThirdParty folder named myapp-settings. Inside of that folder is another folder called src and composer.json.
Here's the content of that composer.json
{
"name": "myapp/settings",
"description": ".",
"license": "MIT",
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Myapp\\Settings\\": "src"
}
},
"require": {}
}
I created a test file inside the src folder named Greet.php
<?php namespace Myapp\Settings;
class Greet
{
public function hello()
{
return 'Hey, there!';
}
}
On codeigniter's App\Config\Autoload.php
public $psr4 = [
'Myapp\Settings' => APPPATH . 'ThirdParty/myapp-settings/src'
];
Then on codeigniter's default controller I called it.
<?php namespace App\Controllers;
use Myapp\Settings\Greet;
class Home extends BaseController
{
public function index()
{
$h = new Greet();
echo $h->hello();
}
//--------------------------------------------------------------------
}
Once I run it I got an error Class 'Myapp\Settings\Greet' not found. APPPATH\Controllers\Home.php at line 9. How can I fix this?
Instead of editing the codeigniter's app/Config/Autoload.php, revert it and add these lines to the project composer.json:
"repositories": [
{
"type": "path",
"url": "app/ThirdParty/myapp-settings"
}
],
// "require": {
Then run composer require myapp/settings
Since I have also stuck at this problem, a GitHub repository is created just for it. You might check and clone, watch the commits to understand the steps.

laravel 4.1.28 command registration is not working

I am currently working with Laravel version 4.1.28 and have created a command with php artisan command:make which works fine and created a file under app/commands/ArchiveMailorder.php:
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class ArchiveMailorder extends Command {
protected $name = 'archive:mailorder';
protected $description = 'This is my first command';
public function __construct()
{
parent::__construct();
}
public function fire()
{
$this->line("Hello world");
}
protected function getArguments()
{
return array(
array(),
);
}
protected function getOptions()
{
return array(
array(),
);
}
}
Now when I try to register this command inside app/start/artisan.php like:
Artisan::add(new ArchiveMailorder());
Or
\Illuminate\Foundation\Artisan::add(new ArchiveMailorder());
Or
\Illuminate\Support\Facades\Artisan::add(new ArchiveMailorder());
I get the following error:
{"error":{"type":"ErrorException","message":"Missing argument 1 for Symfony\\Component\\Console\\Command\\Command::addArgument()","file":"C:\\xampp\\htdocs\\mocs\\vendor\\symfony\\console\\Symfony\\Component\\Console\\Command\\Command.php","line":362}}
I have followed these links for creating and registering a command:
Cron Job with Laravel 4
https://laravel.com/docs/4.2/commands#building-a-command
But still can not make the command work. So please tell me what am I doing wrong? And what shall I do to fix this problem?
Oh, and comoser.json file looks like this:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"laravel/framework": "4.1.*",
"mews/purifier": "dev-master",
"anahkiasen/former": "dev-master",
"laracasts/utilities": "1.0"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"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"
}
In laravel 4.1/4.2 you register command simple just by adding
Artisan::add(new SomeCommandName());
In your command your issue lies with this function:
protected function getArguments()
{
return array(
array(),
);
}
Remove that function unless you're supplying arguments. Since you're returning an array.
Unless you need any of these functions, you'll just need these two functions to get you started, consult the laravel docs for anything else I guess? :
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function fire()
{
// Do stuff
}

Laravel 5 steam condenser

Composer.json
"autoload": {
"classmap": [
"database"
],
"files": [
"vendor/koraktor/steam-condenser/lib/steam-condenser.php"
],
"psr-4": {
"App\\": "app/"
}
},
HomeController
public function index()
{
$server = new SourceServer('80.67.11.46:27025');
try {
$server->rconAuth('abc123');
echo $server->rconExec('status');
}
catch(RCONNoAuthException $e) {
trigger_error('Could not authenticate with the game server.',
E_USER_ERROR);
}
}
I have updated the composer after adding, dump-autoload and tried all the solutions i can find with namespaces and so on.
But can't still use the steam condenser classes, any solution for this ?
The error Class 'App\Http\Controllers\SourceServer' not found denotes the fact that you're inside the App\Http\Controllers namespace and as such it will try to find the SourceServer class within that namespace. Prepend \ to your class name to call it in a global context:
$server = new \SourceServer('80.67.11.46:27025');
Or add this after the namespace declaration at the top of your controller:
use SourceServer;
And remove the class mapping from composer.json because it's not needed. You can read up more on how namespaces work in the PHP Namespaces Documentation.

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