I have a interface
<?php namespace Acme\Billing;
use Stripe;
class StripeBilling implements BillingInterface {
}
But i want to use return Redirect::refresh(); but I'm getting the following error:
Symfony \ Component \ Debug \ Exception \ FatalErrorException
Class 'Acme\Billing\Redirect' not found
So i know I will has to do something like use Illuminate\Routing; but this isn't working and allowing me to use the redirect method.
So how can I do this?
You can use
$redirector = \App::make('redirect');
return $redirector->to('url');
Or use it directly like:
return \App::make('redirect')->to('url');
Or any methods you want but refresh() method may produce a redirect loop in the same page, so be careful about it.
Update: Also remember, when using another class with namespace from another namespace then use a preceding \, i.e. \Acme\Billing\Redirect, if you use a use SomeNameSpace\XXX; without \ from Acme\Billing then composer will think it's Acme\Billing\SomeNameSpace\XXX.
Related
Any of my laravel project api routes does not work. It returns this
my test route:
Route::get('/test', function() {
return 'test';
});
When I open route in my browser directly (any routes even a test route):
Symfony\Component\ErrorHandler\Error\FatalError
with this error: laravel\framework\src\Illuminate\Auth\SessionGuard.php:29
my SessionGuard file:
<?php
namespace Illuminate\Auth;
use Illuminate\Auth\Events\Attempting;
use Illuminate\Auth\Events\Authenticated;
use Illuminate\Auth\Events\CurrentDeviceLogout;
use Illuminate\Auth\Events\Failed;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use Illuminate\Auth\Events\OtherDeviceLogout;
use Illuminate\Auth\Events\Validated;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Contracts\Auth\SupportsBasicAuth;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Cookie\QueueingFactory as CookieJar;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Session\Session;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
use RuntimeException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
class SessionGuard implements StatefulGuard, SupportsBasicAuth // line 29
{
In my artisan console:
PHP Warning: Unexpected character in input: '' (ASCII=1) state=0 in \vendor\laravel\framewor
k\src\Illuminate\Auth\GuardHelpers.php on line 36
My project was working without any problem, and I didnt change anything in my project, but today I have this problem.
Update:
When I create new route, it returns 404 error, cant define new routes, and my old routes still returns old error even when I delete them!
I have been using laravel so far but sometimes I am so confused about choosing correct Request listed bellow.
use Request;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Request;
I have created a test method to my corresponding Route & Controller like bellow.
public function test()
{
dd(Request::all());
}
If I choose use Illuminate\Support\Facades\Request; or use Request; it work's fine and get empty array.
But When I choose use Illuminate\Http\Request;
I get error message saying Request::all() should not be called statically. So, it arises two question in my mind.
What's the difference among them ?
When to use Http Request or Illuminate Support Facades Request. Thanks
public function test(Request $request)
{
dd($request->all());
}
Try This
I have a route in Laravel 7 that saves a file to a S3 disk and returns a temporary URL to it. Simplified the code looks like this:
Storage::disk('s3')->put('image.jpg', $file);
return Storage::disk('s3')->temporaryUrl('image.jpg');
I want to write a test for that route. This is normally straightforward with Laravel. I mock the storage with Storage::fake('s3') and assert the file creation with Storage::disk('s3')->assertExists('image.jpg').
The fake storage does not support Storage::temporaryUrl(). If trying to use that method it throws the following error:
This driver does not support creating temporary URLs.
A common work-a-round is to use Laravel's low level mocking API like this:
Storage::shouldReceive('temporaryUrl')
->once()
->andReturn('http://examples.com/a-temporary-url');
This solution is recommended in a LaraCasts thread and a GitHub issue about that limitation of Storage::fake().
Is there any way I can combine that two approaches to test a route that does both?
I would like to avoid reimplementing Storage::fake(). Also, I would like to avoid adding a check into the production code to not call Storage::temporaryUrl() if the environment is testing. The latter one is another work-a-round proposed in the LaraCasts thread already mentioned above.
I had the same problem and came up with the following solution:
$fakeFilesystem = Storage::fake('somediskname');
$proxyMockedFakeFilesystem = Mockery::mock($fakeFilesystem);
$proxyMockedFakeFilesystem->shouldReceive('temporaryUrl')
->andReturn('http://some-signed-url.test');
Storage::set('somediskname', $proxyMockedFakeFilesystem);
Now Storage::disk('somediskname')->temporaryUrl('somefile.png', now()->addMinutes(20)) returns http://some-signed-url.test and I can actually store files in the temporary filesystem that Storage::fake() provides without any further changes.
Re #abenevaut answer above, and the problems experienced in the comments - the call to Storage::disk() also needs mocking - something like:
Storage::fake('s3');
Storage::shouldReceive('disk')
->andReturn(
new class()
{
public function temporaryUrl($path)
{
return 'https://mock-aws.com/' . $path;
}
}
);
$expectedUrl = Storage::disk('s3')->temporaryUrl(
'some-path',
now()->addMinutes(5)
);
$this->assertEquals('https://mock-aws.com/some-path', $expectedUrl);
You can follow this article https://laravel-news.com/testing-file-uploads-with-laravel, and mix it with your needs like follow; Mocks seem cumulative:
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
public function testAvatarUpload()
{
$temporaryUrl = 'http://examples.com/a-temporary-url';
Storage::fake('avatars');
/*
* >>> Your Specific Asserts HERE
*/
Storage::shouldReceive('temporaryUrl')
->once()
->andReturn($temporaryUrl);
$response = $this->json('POST', '/avatar', [
'avatar' => UploadedFile::fake()->image('avatar.jpg')
]);
$this->assertContains($response, $temporaryUrl);
// Assert the file was stored...
Storage::disk('avatars')->assertExists('avatar.jpg');
// Assert a file does not exist...
Storage::disk('avatars')->assertMissing('missing.jpg');
}
}
Another exemple for console feature tests:
command : https://github.com/abenevaut/pokemon-friends.com/blob/1.1.3/app/Console/Commands/PushFileToAwsCommand.php
test : https://github.com/abenevaut/pokemon-friends.com/blob/1.1.6/tests/Feature/Console/Files/PushFileToCloudCommandTest.php
I am current using this
Excel::create('Export Test',function($excel) use($printData){
$excel->sheet("Paper Print", function($sheet) use($printData){
return $sheet->stringFromColumnIndex(5);
}
}
the problem is it says the method doesnt exist.
I have tried PHPExcel_Cell::stringFromColumnIndex(5);
This says PHPExcel_Cell doesnt exist which would make since.
$excel->stringFromColumnIndex(5) also doesnt work because $excel doesnt exist.
From the documentation these methods should be available from both $excel and $sheet.
You just need to add the \ in front of the PHPExcel_Cell class reference because it doesn't exist in your App\Http\Controllers namespace.
Excel::create('Export Test',function($excel) use($printData){
$excel->sheet("Paper Print", function($sheet) use($printData){
return \PHPExcel_Cell::stringFromColumnIndex(5);
}
}
This ensures you are calling the Class::Method from the Global Namespace - otherwise you could add the line below to the top of your controller to use it without the \ in your anonymous function.
use PHPExcel_Cell;
I wrote custom class with method that returns array.
I need to autoload this class like Auth() class in Laravel, that I could get access to it from any controller not using use
Create one custom helper file
and add function
if (! function_exists('yourcustomclass')) {
function yourcustomclass()
{
use App\Http\yourcustomclassname;
return new yourcustomclassname()
}
}
you can use yourcustomclass() function from anywhere to get yourcustomclassname class object
When accessing class/function from other namespace than you're currently in you have to use Fully-Qualified Class Name (or type use, but you don't want to do that), so instead of Auth::user() you need to write \Auth::user()
\ at the beginning means that class is located in root namespace
Why don't you write super method(s) in App\Http\Controllers\Controller?
Simply call super method(s) in the subclass which extends Controller