I'm new with namespaces and I'm making tests but when I run app.php I get the next error:
Fatal error: Class 'ProtocoloWT\models\status' not found in
C:\xampp\htdocs\wt.uptkd\protocoloWt\app.php
.
app.php
use Neomerx\JsonApi\Encoder\Encoder;
use Neomerx\JsonApi\Encoder\EncoderOptions;
use ProtocoloWT\models\status;
require '../vendor/autoload.php';
$status = status::instance(http_response_code(204));
$encoder = Encoder::instance([
'\status' => '\statusEsquema',
], new EncoderOptions(JSON_PRETTY_PRINT, 'http://example.com/api/v1'));
echo $encoder->encodeData($status);
status.php
namespace ProtocoloWT\models;
class status
{
public static function instance ($status)
{
$estado = new status();
$estado->status = $status;
return $estado;
}
}
composer.json
{
"name": "neomerx/json-api",
"description": "Framework agnostic JSON API (jsonapi.org) implementation",
"keywords": [
"jsonapi.org",
"json-api",
"jsonapi",
"neomerx",
"json",
"api"
],
"homepage": "https://github.com/neomerx/json-api",
"support": {
"issues": "https://github.com/neomerx/json-api/issues"
},
"license": "Apache-2.0",
"authors": [
{
"name": "neomerx",
"email": "info#neomerx.com"
}
],
"require": {
"php": ">=5.5.0",
"psr/http-message": "^1.0",
"psr/log": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^4.6 || ^5.0 || ^6.0",
"mockery/mockery": "~0.9.4",
"scrutinizer/ocular": "^1.3",
"squizlabs/php_codesniffer": "^2.5",
"monolog/monolog": "^1.18",
"phpmd/phpmd": "^2.6"
},
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"Neomerx\\JsonApi\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Neomerx\\Tests\\JsonApi\\": "tests/",
"Neomerx\\Samples\\JsonApi\\": "sample/"
}
},
"scripts": {
"test": ["#test-unit", "#test-cs", "#test-md"],
"test-unit": "./vendor/phpunit/phpunit/phpunit --coverage-text",
"test-unit-with-coverage": "phpdbg -qrr ./vendor/bin/phpunit --coverage-text",
"test-cs": "./vendor/bin/phpcs -p -s --standard=PSR2 ./src ./tests",
"test-md": "./vendor/bin/phpmd ./src text codesize,controversial,cleancode,design,unusedcode,naming",
"perf-php": "docker-compose run --rm cli_php php /app/sample/sample.php -t=10000",
"perf-hhvm": "docker-compose run --rm cli_hhvm hhvm /app/sample/sample.php -t=10000"
}
}
File app.php is in a directory called protocoloWT/ and status.php is in protocoloWT/models.
I installed a framework with composer and I can use those namespaces (Neomerx\JsonApi), but I can't create my own namespaces in my files because I get that error. What could be the issue?
PS: Sorry for my english, I have tried to explain it the best I can
Related
I am trying to integrate firebase/php-jwt into my Laravel project.
I added the following code from the firebase/php-jwt to test it out.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use GuzzleHttp;
use Phpfastcache\CacheManager;
use Firebase\JWT\CachedKeySet;
use Firebase\JWT\JWT;
class MyUserController extends Controller
{
public function searchUser(Request $request)
{
$jwksUri = 'omitted';
// Create an HTTP client (can be any PSR-7 compatible HTTP client)
$httpClient = new Client();
// Create an HTTP request factory (can be any PSR-17 compatible HTTP request factory)
$httpFactory = new GuzzleHttp\Psr7\HttpFactory();
// Create a cache item pool (can be any PSR-6 compatible cache item pool)
$cacheItemPool = CacheManager::getInstance('files');
$keySet = new CachedKeySet(
$jwksUri,
$httpClient,
$httpFactory,
$cacheItemPool,
null, // $expiresAfter int seconds to set the JWKS to expire
true // $rateLimit true to enable rate limit of 10 RPS on lookup of invalid keys
);
$decoded = JWT::decode($jwt, $keySet);
$token = $request->bearerToken();
dd($token);
}
}
But I am getting the following error when I run the code
Class 'Firebase\JWT\CachedKeySet' not found
I found a similar question here and I have tried adding the require statement for vendor/autoload.php and it didn't work for me.
I am using PHP version 7.4.29.
Here is my composer.json
{
"name": "laravel/lumen",
"description": "The Laravel Lumen Framework.",
"keywords": ["framework", "laravel", "lumen"],
"license": "MIT",
"type": "project",
"require": {
"php": "^7.3|^8.0",
"firebase/php-jwt": "^6.1",
"flipbox/lumen-generator": "^8.2",
"guzzlehttp/guzzle": "^7.0",
"itsgoingd/clockwork": "^5.1",
"laravel/lumen-framework": "^8.3.1",
"phpfastcache/phpfastcache": "^8.1",
"symfony/psr-http-message-bridge": "^2.1"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",
"mockery/mockery": "^1.3.1",
"phpunit/phpunit": "^9.5.10"
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"autoload-dev": {
"classmap": [
"tests/"
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-root-package-install": [
"#php -r \"file_exists('.env') || copy('.env.example', '.env');\""
]
},
"symfony/psr-http-message-bridge": "0.2"
}
So how do I fix this?
I have a project to test and play around, with the following structure:
app/
controllers/
HomeController.php
handlers/
models/
vendor/
composer/
psr/
pusher/
pusher-php-server/
src/
Pusher.php
PusherException.php
PusherInstance.php
tests/
composer.json
autoload.php
index.php
I tried to require the Pusher autoloader in my index file:
require 'vendor/autoload.php';
Which is the following:
// autoload.php #generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInite16b90ab01042d2a69b1d54243c9e23a::getLoader();
Now, in my HomeController.php, I have the following code:
namespace App\controllers;
use \App\handlers\Views as View;
use \App\models\Home_model as Home;
use \App\controllers\SessionController;
use \Pusher\Pusher as Pusher;
class HomeController {
private $pusher;
public function __construct() {
$options = array(
'cluster' => 'hmm',
'encrypted' => true
);
$this->pusher = new Pusher(
'secret',
'secret',
'secret',
$options
);
}
public function index() {
$data['message'] = 'hello world';
$this->pusher->trigger('my-channel', 'my-event', $data);
return $this->view->render('views/home/index.php');
}
}
But this returns me an error:
Fatal error: Class 'Pusher\Pusher' not found in
And I'm not sure what I'm doing wrong. Could someone explain me what I'm doing wrong?
In composer.json I get the following:
{
"name": "pusher/pusher-php-server",
"description" : "Library for interacting with the Pusher REST API",
"keywords": ["php-pusher-server", "pusher", "rest", "realtime", "real-time", "real time", "messaging", "push", "trigger", "publish", "events"],
"license": "MIT",
"require": {
"php": "^5.4 || ^7.0",
"ext-curl": "*",
"psr/log": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8 || ^5.7"
},
"autoload": {
"psr-4": {
"Pusher\\": "src/"
}
},
"autoload-dev": {
"psr-4": { "": "tests/" }
},
"config": {
"preferred-install": "dist"
},
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
On Github, they mention that the library depends on cURL and JSON modules.
Not realy sure if this has something to do with my issue?
I'm still stuck, so any help is greatly appreciated.
Also, I'm using a .htaccess file, to rewrite my urls.
I have managed your code to work with right composer.json next to index.php:
{
"name": "awesome/project",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "Author",
"email": "author#gmail.com"
}
],
"autoload": {
"psr-4": {
"": ""
}
},
"require": {
"pusher/pusher-php-server": "dev-master"
}
}
Then just run composer install.
My index.php contents:
<?php
require 'vendor/autoload.php';
use app\controllers\HomeController;
$ctrl = new HomeController();
I'm currently trying to build my first API Wrapper for Laravel but having some Problems with auto loading the actual PHP-Lirbary.
I'm receiving the following error when I try to Access the Widget in my view.
Error:
ErrorException (E_ERROR)
Class 'Uploadcare\Api' not found
View
{!! app()->uploadcare->widget->getInputTag('file_input') !!}
My package ServiceProvider is looking like this.
UploadcareServiceProvider
use Illuminate\Support\ServiceProvider;
use Uploadcare\Api as Uploadcare;
class UploadcareServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
$this->publishes([
__DIR__.'/config/uploadcare.php' => config_path('uploadcare.php'),
]);
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->singleton('uploadcare', function($app) {
$publicKey = config('uploadcare.public_key');
$privateKey = config('uploadcare.private_key');
return new Uploadcare($publicKey, $privateKey);
});
}
/**
* Get the services provided by the provider.
*
* #return array
*/
public function provides()
{
return [
Uploadcare::class,
];
}
}
And in my packages composer.json file im requring the package.
Packages composer.json
"require": {
"uploadcare/uploadcare-php": "^2.1"
}
In the Vendor folder of the package I'm able to find the Uploadcae\Api Class so i'm not sure why I'm receiving this error.
packages composer.json
{
"name": "company/uploadcare",
"description": "An uploadcre integration for Laravel",
"type": "library",
"authors": [
{
"name": "Stan Barrows",
"email": ""
}
],
"require-dev": {
"orchestra/testbench": "^3.5",
"phpunit/phpunit": "^6.3"
},
"autoload": {
"psr-4": {
"Company\\Uploadcare\\": "src/"
}
},
"autoload-dev": {
},
"extra": {
"laravel": {
"providers": [
"Company\\Uploadcare\\UploadcareServiceProvider"
]
}
},
"require": {
"uploadcare/uploadcare-php": "^2.1"
}
}
main project composer.json file
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"repositories": [
{
"type": "path",
"url": "packages/company/uploadcare"
}
],
"require": {
"php": ">=7.0.0",
"fideloper/proxy": "~3.3",
"laravel/framework": "5.5.*",
"laravel/tinker": "~1.0",
"laravelnews/laravel-twbs4": "^1.3",
"ramsey/uuid": "^3.7"
},
"require-dev": {
"filp/whoops": "~2.0",
"fzaninotto/faker": "~1.4",
"laravel/dusk": "^2.0",
"mockery/mockery": "~1.0",
"phpunit/phpunit": "~6.0",
"symfony/thanks": "^1.0"
},
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"Genusshaus\\": "app/",
"Smart6ate\\Roles\\": "packages/company/roles/src",
"Smart6ate\\Uploadcare\\": "packages/company/uploadcare/src/"
},
"files": ["tests/Helpers/functions.php"]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"dont-discover": [
]
}
},
"scripts": {
"post-root-package-install": [
"#php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"#php artisan key:generate"
],
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover"
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
}
}
I have this in my composer.json
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=7.0.0",
"alchemy/zippy": "^0.4.8",
"barryvdh/laravel-debugbar": "^3.1",
"fideloper/proxy": "~3.3",
"graham-campbell/exceptions": "^10.0",
"intervention/image": "^2.4",
"intervention/imagecache": "^2.3",
"laravel/framework": "5.5.*",
"laravel/tinker": "~1.0",
"laravelcollective/html": "^5.5",
"symfony/dom-crawler": "^3.3"
},
"files": [
"vendor/redbutton/text-image-alpha/vendor/autoload.php"
],
"require-dev": {
"filp/whoops": "^2.1",
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~6.0"
},
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"dont-discover": [
]
}
},
"scripts": {
"post-root-package-install": [
"#php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"#php artisan key:generate"
],
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover"
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
}
}
The php files for my package are in a laravel installation:
/vendor/redbutton/text-image-alpha/src/
When I try to call a file I get the message:
Class 'Class 'RedButton\TextImageAlpha\TextImageAlpha' not found' not found
I use this to try and call the class:
$image = new \RedButton\TextImageAlpha\TextImageAlpha( 'some string' );
The file in /vendor/redbutton/text-image-alpha/src/TextImageAlpha.php looks like this:
<?php
namespace RedButton\TextImageAlpha;
use RedButton\TextImageAlpha\Exceptions;
use RedButton\Tools\Objects;
/**
* TextImageAlpha class convert a text to image.
*
* #author Tomas Rathouz <trathouz at gmail.com>
*/
class TextImageAlpha
{
// lots of code
}
This is my first composer package and I don't really have an idea of what is going wrong here. Could someone please explain to me what I'm doing wrong?
Good news. I've looked at your package at Bitbucket and you're doing it right in its composer.json:
Drop unnecessary code from composer.json
I've noticed in pasted composer.json there is manual adding of some vendor file.
"files": [
"vendor/redbutton/text-image-alpha/vendor/autoload.php"
],
/vendor nested in /vendor doesn't make sense as it might duplicate autoloading and break it.
Also I don't see redbutton/text-image-alpha in require section, where it should be.
How to add package the right way?
To install your package just call composer:
composer require redbutton/text-image-alpha
And that's it.
It should appear in require section in composer.json. Composer will autoload it by rules in composer.json of the package - here.
Test
I've tried following code and class is found correctly:
<?php
require __DIR__ . '/vendor/autoload.php';
$image = new \RedButton\TextImageAlpha\TextImageAlpha('some string');
var_dump($image);
Having composer.json:
{
"require": {
"redbutton/text-image-alpha": "^1.0"
}
}
I am following the directions given here to install neoclient on Windows 7 -
https://github.com/neoxygen/neo4j-neoclient
But I get the error -
<b>Parse error</b>: syntax error, unexpected 'use' (T_USE) in <b>C:\xampp\htdocs\send.php</b> on line <b>7</b><br />
when I try to run the sample php code.
My code is -
<?php
try
{
require_once 'vendor/autoload.php';
use Neoxygen\NeoClient\ClientBuilder;
$client = ClientBuilder::create()
->addConnection('default','http','localhost',7474)
->build();
$version = $client->getNeo4jVersion();
}
catch(Exception $e)
{
echo $e->getMessage();
}
echo $version;
?>
composer.json -
{
"name": "neoxygen/neoclient",
"type": "library",
"description": "NeoClient is the most advanced Http Client for Neo4j",
"keywords": [
"graph",
"neo4j",
"cluster",
"client",
"high-availibility"
],
"homepage": "http://neoxygen.io",
"license": "MIT",
"authors": [
{
"name": "Christophe Willemsen",
"email": "chris#neoxygen.io"
}
],
"require": {
"php": ">= 5.5",
"guzzlehttp/guzzle": "^6.0",
"monolog/monolog": "~1.1",
"symfony/yaml": "^2.7",
"symfony/config": "^2.7",
"symfony/dependency-injection": "^2.7",
"symfony/event-dispatcher": "^2.7",
"graphaware/neo4j-response-formatter": "^1.0"
},
"require-dev": {
"phpspec/phpspec": "~2.0",
"phpunit/phpunit": "4.*",
"bossa/phpspec2-expect": "*",
"behat/behat": "~3.0"
},
"autoload": {
"psr-4": {
"Neoxygen\\NeoClient\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Neoxygen\\NeoClient\\Tests\\": "tests/Neoxygen/NeoClient/Tests"
}
},
"extra": {
"branch-alias": {
"dev-master": "3.1-dev"
}
}
}
I'm the maintainer of NeoClient.
I don't think I have ever seen this piece of code somewhere, please point to a link.
Especially, require and use statements should be in the beginning of the file, after the open tag.
Secondly, you don't need to instantiate the client in a try/catch block.
Here is the correct piece of code :
<?php
require_once 'vendor/autoload.php';
use Neoxygen\NeoClient\ClientBuilder;
$client = ClientBuilder::create()
->addConnection('default','http','localhost',7474)
->build();
try
{
$version = $client->getNeo4jVersion();
}
catch(Exception $e)
{
echo $e->getMessage();
}
echo $version;
?>