PHP class not found in Laravel controller - php

I'm having an issue within a Laravel controller, I'm not sure where to begin troubleshooting. Basically 2 separate methods are calling the same external class; the process method works, however when calling process3d I run into the error class 'App\Services\PaymentGateway\Gateway\RequestGatewayEntryPointList' not found.
DebugController.php
namespace App\Http\Controllers;
use App\Services\PaymentGateway\Gateway\RequestGatewayEntryPointList;
...
public function process(Request $request) {
...
$rgeplRequestGatewayEntryPointList = new RequestGatewayEntryPointList;
...
}
public function process3d(Request $request) {
...
$rgeplRequestGatewayEntryPointList = new RequestGatewayEntryPointList;
...
}
PaymentSystem.php
namespace App\Services\PaymentGateway\Gateway;
...
class RequestGatewayEntryPointList {
...
}
I've omitted irrelevant code to keep the question brief, but I'm of course happy to provide more details if it will help.
What's going on?

Found it 5 minutes after posting to stackoverflow... oops.
process3d was missing require_once DIR.'/../../Services/PaymentGateway/Gateway/PaymentSystem.php';
Adding this solved the issue.

Related

Avoid Laravel facade on controller

I'm using Laravel 5.5 and trying to get used to code by psr-2 standard (just started learning). I analyze all my code with Quafoo QA and go step by step fixing the errors and record them.
By using facades i get this error "Avoid using static access to class". Because of it i'm trying to avoid using them.
On my controller i have this code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Events\FileLoaded;
use Illuminate\Support\Facades\Input;
use Illuminate\Auth\Middleware\Authenticate;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use \Illuminate\Contracts\View\Factory as ViewFactory;
class LoadDataController extends Controller
{
public function index()
{
$viewfactory = app(ViewFactory::class);
return $viewfactory->make('LoadData/index');
}
//more code
}
Besides the View Facade i also use DB, Input, Validator and Storage
Is this the correct way, are there others?
You don't need to avoid Facades - they are a key part of the framework. But if you want to, you can use dependency injection to include the classes you need as arguments in the controller methods:
class LoadDataController extends Controller
{
public function index(ViewFactory $viewFactory)
{
return $viewfactory->make('LoadData/index');
}
//more code
}
Or if you need that in all the controller methods:
class LoadDataController extends Controller
{
private $viewFactory;
public function __construct(ViewFactory $viewFactory)
{
$this->viewFactory = $viewFactory;
}
public function index()
{
return $this->viewFactory->make('LoadData/index');
}
//more code
}
Of course, this doesn't actually change the functionality of the code you've written, it just rearranges it. I wouldn't take the word of the code analyzer you mentioned as something you are doing wrong. These are standard patterns to use in Laravel.

Call to undefined method Session::has() in Laravel 5 + Codeception functional testing

I was running some functional tests in Laravel 5 with Codeception.
Below is a sample test
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class HomePageTestCest
{
use WithoutMiddleware;
public function _before(FunctionalTester $I)
{
//some setup to run
}
public function _after(FunctionalTester $I)
{
}
// tests
public function tryToTest(FunctionalTester $I)
{
$I->wantToTest('If i can go to home page without errors');
$I->amOnPage('/');
$I->canSee('WELCOME TO HOMEPAGE');
}
}
But i am getting below error while running the test:
Fatal error: Call to undefined method Session::has() in /var/www/laravel5/storage/framework/views/e7953b3ce9b90e51d4cfdb279790953bbe25dc9a.php on line 225
And in line 225 i have:
<?php if(Session::has('someKey')): ?>
//some html code
<?php endif; ?>
I think the problem is with session driver. I also tried to implement this but nothing changed.
Hope will get some help. Thanks in advance.
In LARAVEL 5.2 they also create the session class like helper
where in you can make use of it like this
session()->has('key')
See DOCUMENTATION SESSION
namespace App\Http\Controllers;
// import session class using alias
use Session;
class SampleController exntends Controller {
public function __construct() {
}
public function index() {
// print session values
var_dump(Session::all());
}
}
Thanks to hamedmehryar for the solution. I dont know why but for some reason i have to define whole namespace for the facades, even they are already defined in 'aliases' inside config/app.php. For example:
Session::has()
should be like
Illuminate\Support\Facades\Session::has()
I explained it here so if anyone face this problem will be able to solve it from here.
Do not forget
use Illuminate\Http\Request;.
AND use session like this
$request->session()->has('key');

How To Implement the Driver Pattern Using Helpers in Lumen

How would I go about implementing a driver pattern in Lumen? Right now I have a helper ResponseHandler.php in /app/Helpers which defines an abstract class ResponseHandler.
// app/Helpers/ResponseHandler.php
namespace App\Helpers;
use \Symfony\Component\HttpFoundation\Response as HTTPResponse;
abstract class ResponseHandler extends HTTPResponse
{
abstract public function success();
abstract public function fail();
[...]
}
I have drivers defined that extend ResponseHandler in the subdirectory /app/Helpers/Response. A driver is defined as follows:
// app/Helpers/Response/JSON.php
namespace App\Helpers\ResponseHandler;
class JSON extends ResponseHandler
{
public function fail() {
// logic
}
public function success() {
// logic
}
[...]
}
The problem I'm running into is that when I try to use the driver inside a function in my controller, Lumen throws the following error: Class 'App\Helpers\ResponseHandler\JSON' not found. This is the controller I've written (irrelevant parts removed):
// app/Http/Controllers/ResponseController.php
namespace App\Http\Controllers;
use App\Helpers\ResponseHandler\JSON as Response;
class ResponseController extends Controller
{
public function returnSomething($content) {
[...]
return Response::success($_ProcessedContent);
}
[...]
}
I've tried changing namespaces around which just ends up causing more errors and doesn't end up solving this one. I suspect that I'm just not familiar enough with namespaces and how Lumen uses them...but I've been working on this problem for a few hours now and can't seem to figure it out.
Could someone with more experience with Lumen/Laravel shed some light onto this issue for me?
* [SOLUTION] *
The design pattern was correct, but I needed to run:
composer dump-autoload
after everything was written.
Have you tried running: composer dumpautoload from the command line?

Attempting to use custom facade results in 'Call to undefined method' on Application::create() in Laravel?

I am attempting to create my own custom facade for a Search feature, but I'm having a little difficulty:
type: Symfony\Component\Debug\Exception\FatalErrorException
message: Call to undefined method Illuminate\Foundation\Application::create()
file: H:\myproj\vendor\laravel\framework\src\Illuminate\Container\Container.php
line: 165
This error is caused by my code hitting:
Search::indexObject();
Where my Search facade is set up as follows:
SearchServiceProvider
<?php
namespace MyProj\Search;
use Illuminate\Support\ServiceProvider;
class SearchServiceProvider extends ServiceProvider {
public function register() {
$this->app->bind('search', 'MyProj\Search\Search');
}
}
Search Facade
<?php
namespace MyProj\Facades;
use Illuminate\Support\Facades\Facade;
class Search extends Facade {
public static function getFacadeAccessor() {
return 'search';
}
}
Search class
<?php
namespace MyProj\Search;
use Elasticsearch\Client;
use Credential;
class Search {
private $elasticSearchClient;
public function __construct() {
$this->elasticSearchClient = new Client(array(
'hosts' => [Credential::ElasticSearchHost]
));
}
public function indexObject($object) {
// Code
return $this->elasticSearchClient->index($params);
}
public function get() {
return $this->$elasticSearchClient;
}
}
I have run composer dump-autoload without success, and my facade and service provider is loaded in app.php as follows:
Aliases array
'Search' => 'MyProj\Facades\Search',
Providers array
'MyProj\Search\SearchServiceProvider'
I've spent the past 30 minutes debugging and searching for this error without any fix. What's going on here?
EDIT: I've added in the stack trace, which you can see below. Additionally, I can see that getFacadeAccessor() is being called correctly, but anything beyond that is outside of my understanding.
The highlighted frame represents the last occurrence of normal operation, both frames on Handler.php represent the formatting and outputting of the error at the top of the question.
Appreciate this is a bit of an old thread, however you'd get the problems described if you compiled cache wasn't cleared after adding your new Facade.
You should run:
php artisan clear-compiled

Symfony2: pthreads produces blank page

I'm trying to use the extension php_pthreads with Symfony2.
I set up a simple controller:
<?php
namespace Test\TestBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class SimpleThread extends \Thread {
public function run() {
sleep(100);
}
}
class DefaultController extends Controller
{
public function indexAction()
{
$simpleThread = new SimpleThread();
$simpleThread->start();
return $this->render('TestTestBundle:Default:index.html.twig', array('name' => "World"));
}
}
And it just gives me a blank page (ERR_CONNECTION_RESET). I have nothing to try to fix it since there is no error, I don't know how to debug this.
When I try to run this simple test code (https://stackoverflow.com/a/15501449/3070307) outside of Symfony, it works fine.
Any help is much appreciated.
EDIT:
Ok two things:
sleep(100);
Sleeps for 100 seconds... I miss-typed usleep.
Second, the Symfony cache is messing with thread execution apprently, disabling it fixed it (http://symfony.com/doc/current/cookbook/debugging.html).
The only remaining problem is the render function does nothing, I'm not even sure it's called.

Categories