Laravel 5.2 + Whoops - php

I've previously had Whoops in 5.1 and 5.0; but since 5.2 the implementation I used earlier no longer works.
I have been unable to find a way to implement Whoops 2.0 to Laravel 5.2 as is.
Any suggestions?

Just add this method to your app/Exceptions/Handler.php file, it overrides the existing method that would generate the Symfony error response. If the app is in config mode, it will return the Whoops response. If you're build some sort of API, you might instead want to use the JsonResponseHandler over the PrettyPageHandler which would give you a nice JSON response for exceptions.
/**
* Create a Symfony response for the given exception.
*
* #param \Exception $e
* #return mixed
*/
protected function convertExceptionToResponse(Exception $e)
{
if (config('app.debug')) {
$whoops = new \Whoops\Run;
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
return response()->make(
$whoops->handleException($e),
method_exists($e, 'getStatusCode') ? $e->getStatusCode() : 500,
method_exists($e, 'getHeaders') ? $e->getHeaders() : []
);
}
return parent::convertExceptionToResponse($e);
}

Whoops 2.1 was deployed 4 days ago. I just tried with Laravel 5.2 and it worked just fine.
I just followed Matt Stauffer's tutorial.
https://mattstauffer.co/blog/bringing-whoops-back-to-laravel-5

Related

I am getting parse_str() error while implementing facebook login with laravel socialite

I am implementing facebook login with socialite laravel but after successful login with facebook when it return me back to callback url i receive an error
laravel version 5.6
Socailite version 3.0
php version 7.2
public function redirectToProvider()
{
return Socialite::driver('facebook')->redirect();
}
/**
* Obtain the user information from facebook.
*
* #return \Illuminate\Http\Response
*/
public function handleProviderCallback()
{
$user = Socialite::driver('facebook')->user();
return $user->token;
}
Erros look like this.
parse_str(): Calling parse_str() without the result argument is deprecated
C:\xampp\htdocs\ecommerce\vendor\laravel\socialite\src\Two\FacebookProvider.php
Routes.php
// FACEBOOK ROUTES
Route::get('login/facebook', 'Auth\LoginController#redirectToProvider');
Route::get('login/facebook/callback', 'Auth\LoginController#handleProviderCallback');
// FACEBOOK ROUTES END HERE
parse_str(): https://www.php.net/manual/en/function.parse-str.php
Using this function without the result parameter is highly DISCOURAGED and DEPRECATED as of PHP 7.2.
Looking at the version history for the FacebookProvider class on Github the error only seems to exist in version 3.0.0, however, there is not mention of parse_str at all in 3.0.4.
To fix your issue you can run:
composer require laravel/socialite:^3.0.4

Laravel 5.7: Custom blade template for Maintenance Mode but not 503.blade.php

Every time php artisan down is turned on, Laravel displays 503 page.
OK. I can customise it by creating new file called 503.blade.php inside resources/views/errors.
The point is that I don't consider Maintenance Mode as error at any point despite of the fact that it makes website unavailable for a client.
The 503 Service Unavailable error is a server-side error, meaning the
problem is usually with the website's server. ... Even though the 503
Service Unavailable error means that there's an error on another
computer, the issue is probably only temporary.
How can I define my own blade template (let's say maintenance_mode.blade.php) to customize what users see during the app down and leave 503 intact?
My efforts: I investigated the middleware itself inside the vendor but it only throws the exception, I assume the exception is being caught somewhere with and handles response with a corresponding view? Can someone point me on how to achieve what I need?
Thanks
One way could be to change render method in Exception's Handler. Something like:
// app_path('Exceptions/Handler.php');
/**
* Render an exception into an HTTP response.
*
* #param \Illuminate\Http\Request $request
* #param \Exception $exception
* #return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
if ($exception instanceof \Illuminate\Foundation\Http\Exceptions\MaintenanceModeException) {
return response()
->view('maintenance.down', [
'message' => 'Come back later.'
], 200)
->header('Content-Type', 'text/html; charset=utf-8');
}
// in Laravel 8.x MaintenanceModeException is deprecated and one should rely on
// #throws \Symfony\Component\HttpKernel\Exception\HttpException
// or
// \Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException
if ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException ) {
return response()
->view('maintenance.down', [
'message' => 'Come back later.'
], 200)
->header('Content-Type', 'text/html; charset=utf-8');
}
return parent::render($request, $exception);
}
Thanks to #Ifnot 's input, answer is updated to comply Laravel 8.x
If You want to display custom message in server maintanence(503.blade.php)
Laravel Has the out of box customization
php artisan down --message="We are Upgrading look and feel"
now We are Upgrading look and feel will be displayed in page while user visiting the page
If You want More customization kindly Look up the package
https://github.com/MisterPhilip/maintenance-mode
If this answer is irrelevnt or not fixed your problem kindly comment below so that i can fix that
Hope it helps
Edited
Ok then run the command in your terminal
php artisan vendor:publish and choose 0 so that it will publish all the views and configs
now open your view folder there will be a errors
folder and you can see the list of error files provided by laravel now change as per your customization and run php artisan opt:clear it will clear all the cache views ,configs and now try it
by Customising your 503.blade.php its works fine form me now
you can just view the tutorial of customising 404.blade.php and customize as per the requirement
Customize 404 in laravel

Twig render empty page with slim framework

I'm using Twig template engine with slim framework 3
i have a simple function inside my controller
public function test_twig($request, $response, $args) {
return $this->view->render($response, "login.phtml");
}
When i run this code in the locahost it works perfectly, but i tried it in my server it shows an empty page, very wierd behavior the page has no dynamic variables it's just HTML
I've tried this also :
$str = $this->view->fetchFromString('<p>Hi, my name is {{ name }}.</p>', [
'name' => "oussama"
]);
$response->getBody()->write($str);
return $response;
It worked in the localhost and not in my server ( i have PHP 5.6 in my server )
After following the execution i ended up here the compile function can not compile a simple HTML file !!!
/**
* Compiles a template source code.
*
* #return string The compiled PHP source code
*
* #throws Twig_Error_Syntax When there was an error during tokenizing, parsing or compiling
*/
public function compileSource(Twig_Source $source)
{
try {
return $this->compile($this->parse($this->tokenize($source)));
} catch (Twig_Error $e) {
$e->setSourceContext($source);
throw $e;
} catch (Exception $e) {
throw new Twig_Error_Syntax(sprintf('An exception has been thrown during the compilation of a template ("%s").', $e->getMessage()), -1, $source, $e);
}
}
I did follow the code till Twig_Lexer Class Inside the Constructor
Exactly in this line :
'operator' => $this->getOperatorRegex(),
Twig since version v2.0.0 has required PHP version 7.0 or above. As you have stated that you have PHP 5.6 on the remote server, they will not work together. You can either upgrade to PHP 7 on the remote (if possible), or downgrade twig to v1.35.0, which still supports PHP 5.
Source:
https://packagist.org/packages/twig/twig#v2.2.0
https://github.com/twigphp/Twig/blob/29bb02dde09ff56291d30f7687eb8696918023af/composer.json
Twig 2.4 requires PHP 7 and above. Your server does not satisfy that requirement and Twig fails.
We need to override the default Twig dependency by setting our own. Add a Twig dependency in your project conposer.json file:
"twig/twig":"^1.18"
Tell composer to run the new configuration and apply the changes:
composer update
I just made this working by changing my composer.json requires:
"slim/twig-view": "2.0", change the version to 2.0 since it works with PHP 5.6
Also there was and issue with Eloquent if you use the version "~5.1" it won't work i just putted a strict version "5.4.36" for Database Dependency like this :
"illuminate/database": "5.4.36"

Laravel 5.5 Unit testing controller method

I am trying to setup a simple unit test for one my controller method.
The goals is to test if the a view has an expected value.
/**
* Does the homepage receive all companies when there is no licensekey provided.
*
* #return void
*/
public function testAllCompaniesOnHomepageWithoutLicensekey()
{
$this->call('GET', '/');
$allCompanies = Company::all();
$this->assertViewHas('allCompanies', $allCompanies);
}
In my conosle I get the following error:
Error: Call to undefined method
Tests\Unit\ExampleTest::assertViewHas()
I am not sure if this is no longer availbale in Laravel 5.5?
Anyone knows how I can test my goal?
Are you migrating from an older Laravel version? There have been changes to Laravel's browser testing in Laravel 5.4 https://laravel.com/docs/5.4/upgrade
In Laravel 5.5 you could try:
$response = $this->get('/');
$allCompanies = Company::all();
$response->assertViewHas('allCompanies', $allCompanies);

symfony2 phpunit - functional testing error (host_with_path)

So thanks to Matteo (phpunit in symfony2 - No tests executed) I can now test my functional tests.
Now I got the following error when running phpunit -c app:
You must change the main Request object in the front controller (app.php)
in order to use the `host_with_path` strategy.
so I did change it in the app.php, from:
$request = RequestFactory::createFromGlobals('host_with_path');
to:
$request = Request::createFromGlobals();
I also updated my swiftmailer-bundle from version 2.3 to 5.4.0.
Unfortunately This did not fix my error.
and this is my ../app/config_test.yml
swiftmailer:
disable_delivery: true
Am I missing something here?
I cannot seem to find this error anywhere on the web. Does someone know how I should fix this error?
After some searching I noticed that the app.php wasn't the problem. It was the DefaultControllerTest.php. The error could be fixed by removing the following lines from the DefaultControllerTest:
$crawler = $client->request('GET', '/hello/Fabien');
$this->assertTrue($crawler->filter('html:contains("Hello Fabien")')->count() > 0);
Due to recent developments our development team decided to stop using Sonata. As a side effect this bug got fixed. So I won't have a solution for this problem.
The problem here is, that the Client object is using neither app.php nor app_dev.php.
The client creates the request internally. So it won't be the request you need.
The only solution I can see is to override the method Symfony\Bundle\FrameworkBundle\Test\WebTestCase::createClient to return your own client. This client is than responsible for creating the actual request object. The following is the current behavior.
namespace Symfony\Component\HttpKernel;
use Symfony\Component\BrowserKit\Client as BaseClient;
class Client extends BaseClient
{
...
/**
* Converts the BrowserKit request to a HttpKernel request.
*
* #param DomRequest $request A DomRequest instance
*
* #return Request A Request instance
*/
protected function filterRequest(DomRequest $request)
{
$httpRequest = Request::create($request->getUri(), $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $request->getServer(), $request->getContent());
foreach ($this->filterFiles($httpRequest->files->all()) as $key => $value) {
$httpRequest->files->set($key, $value);
}
return $httpRequest;
}
...
}
You have to override method filterRequest to return kind of a request you want.

Categories