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
Related
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.
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.
Code
class Rechner {
public function add($a, $b){
return $a + $b;
}
My TestCase
class RechnerTest extends PHPUnit\Framework\TestCase
{
public $calculator;
protected function setUp ()
{
$this->calculator = new Rechner();
}
protected function tearDown ()
{
$this->calculator = NULL;
}
public function testAdd ()
{
$result = $this->calculator->add(1, 2);
$this->assertEquals(3, $result);
}
}
Error Output
Error: Class 'Rechner' not found
Composer.json file
{
"autoload":{
"psr-0" :{
"":"src"
},
"classmap": [
"tests"
]
},
"require": {
"phpunit/phpunit": "^6.5",
"authorizenet/authorizenet": "^1.9"
}
}
My Problem
I am trying to figure out why the Method is not found.
If i try to require __DIR__ the 'Recher.php' does not show up, only the test case...and thats the one i don't need.
Im am not sure what to do.
I am creating a laravel 5.2 package, following are my files:
packages/
-Shreeji/
--Ring/
---composer.json
---src/
----Ring.php
----RingModel.php
----RingServiceProvider
----Views/
-----ringslist.blade.php
composer.json
{
"name": "shreeji/ring",
"description": "Simple",
"license": "MIT",
"authors": [
{
"name": "author",
"email": "email#gmail.com"
}
],
"autoload": {
"psr-4": {
"Shreeji\\Ring\\": "src/"
}
},
"minimum-stability": "dev",
"require": {
"Illuminate/support": "~5"
}
}
Ring.php
namespace Shreeji\Ring;
use Illuminate\Http\Response;
Class Ring {
function __construct() {
}
public function get_all()
{
return view("ring::ringlist");
}
}
RingServiceProvider.php
namespace Shreeji\Ring;
use Illuminate\Support\ServiceProvider;
Class RingServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('ring', function($app){
return new Ring;
});
}
public function boot()
{
$this->loadViewsFrom(__DIR__ . '/Views', 'ring');
}
}
ringlist.blade.php
<!DOCTYPE html>
<html>
<body>
<h1>Welcome</h1>
</body>
</html>
And in app/Http/Controllers I have created a test file like this:
Ringcontroller.php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Shreeji\Ring\Ring;
class RingController extends Controller
{
public function index()
{
$ring = New Ring();
$ring->get_all();
}
}
When I call the controller, browser keeps loading and crashed systematically. I don't know if I can use view outside any controller class like such.
Let me know if I did any mistake in calling view from Ring.php file.
Couple issues I see:
You want to use views, but your package does not require the illuminate/view package. You need to update your composer.json file to require "illuminate/view": "~5".
The view() function is a helper method included at Illuminate\Foundation\helpers.php. Unless you want to depend on the entire Laravel framework just for this function, you will need to create your own view() function. The code is below, where you put it is up to you.
if (! function_exists('view')) {
/**
* Get the evaluated view contents for the given view.
*
* #param string $view
* #param array $data
* #param array $mergeData
* #return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
*/
function view($view = null, $data = [], $mergeData = [])
{
$factory = app(ViewFactory::class);
if (func_num_args() === 0) {
return $factory;
}
return $factory->make($view, $data, $mergeData);
}
}
Once you get the view stuff working, you can make views all day long, but if you don't return anything from your controller, you're not going to see anything. Make sure you return something from your controller methods.
You can use somethin like view composer Docs
In your RingServiceProvider register a composer
use Illuminate\Contracts\View\Factory as ViewFactory;
public function boot(ViewFactory $view)
{
$view->composer('*', 'App\Http\ViewComposers\SomeComposer');
}
And in App\Http\ViewComposers\SomeComposer
use Illuminate\Contracts\View\View;
public function compose(View $view)
{
$view->with('count', '1');
}
Play around with it, basically I am using it share $variables on particular views but maybe this can help you achieve what you want.
Or u can just use Illuminate\Contracts\View\View; to load your view that you need!
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.