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');
Related
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.
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.
I'm using Laravel 5.2 and I've followed the instructions stated here.
However, when I attempt to access it in my controller, I get an error:
Class 'App\Http\Controllers\IPBWI' not found # line 12
<?php
namespace App\Http\Controllers;
use Haslv\Ipbwi;
MyController extends Controller {
public function index() {
$member_info = IPBWI::member()->info(); //line 12
//etc
}
}
I understand what's wrong but I don't understand how do to correctly reference it.
Could you help me out?
I'm not sure where you got this, but I would take it out:
use Haslv\Ipbwi;
If you want to use Laravel's facade and you followed the instructions on the github page, then you should add this to the top of your controller:
use IPBWI;
This is also case-sensitive so make sure that it matches the case in this line of code in your config/app.php file:
'IPBWI' => 'Haslv\Ipbwi\Facade',
You need to move your namespace and use statement above the class declaration.
<?php
namespace App\Http\Controllers;
use Haslv\Ipbwi;
class MyController extends Controller {
// controller code
}
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
I've created a very basic app in Laravel 4, it's something I'll be reusing a lot in various projects so it made sense to convert it to a package before i got too far, but I'm struggling to make the changes to get it working, which I think is largely due to figuring out how to access the various objects that are normally available in an app, eg View::make
I had the following code working in an app:
class PageController extends BaseController {
public function showPage($id)
{
//do stuff
return View::make('page/showPage')
->with('id', $id)
->with('page', $page);
}
for the package I have the following:
use Illuminate\Routing\Controllers\Controller;
use Illuminate\Support\Facades\View;
class PageController extends Controller {
public function showPage($id)
{
//do stuff
return View::make('page/showPage')
->with('id', $id)
->with('page', $page);
}
However this does not load the blade template which is located at:
workbench/packagenamespace/package/src/views/page/showPage.blade.php
nor does this work:
return View::make('packagenamespace/package/src/page/showPage')
Also, I am wondering if what i have done with the use statements where I use the facade object correct, to me it seems like there should be a neater way to access things like the View object?
You should read the docs: http://four.laravel.com/docs/packages
Specifically the part explaining loading views from packages ;)
return View::make('package::view.name');
If you don' want to use:
use Illuminate\Support\Facades\View;
Just do:
use View;
Or even without the use statement:
\View::make('package::view.name');
// Serviceprovider.php
$this->loadViewsFrom(__DIR__.'/resources/views', 'laratour');
// Controller
<?php
namespace Mprince\Laratour\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class LaratourController extends Controller
{
public function index()
{
return view('laratour::index');
// return view('packageName::bladeFile');
}
//END
}