$request->route() returns null in laravel - php

I have this code:
class TotersProviderLoginController extends Controller
{
private $oauthService;
public function __construct(Request $request)
{
$provider = $request->route()->parameter('provider'); // error here
if($provider == 'google')
$this->oauthService = new GoogleOauthService();
else
throw new \Exception('Provider '.($provider ?? '').' not supported!');
}
I have the following routes defined:
Route::get('login/toters/{provider}', 'Accounts\TotersProviderLoginController#redirectToProvider');
Route::get('login/toters/{provider}/redirect', 'Accounts\TotersProviderLoginController#handleProviderCallback');
Route::get('login/toters/{provider}/csrf', 'Accounts\TotersProviderLoginController#getCsrf');
Route::post('login/toters/{provider}/oauth', 'Accounts\TotersProviderLoginController#requestToken');
for some reason when I run
php artisan route:list --verbose
I get this error
In TotersProviderLoginController.php line 38:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Call to a member function parameter() on null
so it's clear that $request->route() is returning null. Why is that?
note: I'm using Laravel 5.8

I have debug myself as you mentioned in the question and i found the way where you get the error.
When you run php artisan route:list --verbose command it will debug all routes and also call controller methods of every routes.
In your case what happens when you run command with verbose, route do not have provider default value and that's why it always gives null value.
While you call routes via postman or web it will definitely work, because at that time you have always some value for provider.
Thanks:)

Related

Laravel - Parse error in /laravel/framework/src/Illuminate/Foundation/helpers.php

I made loads of changes within my code and unfortunately kind of lost track what were the last one.. but I guess one of them created this weird problem.
When I'm trying to run php artisan serve, I get this error -
Parse error: parse error in /Users/Guest/admanager/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php on line 500
And the function that returns the error in helpers.php looks like this -
function factory()
{
$factory = app(EloquentFactory::class);
$arguments = func_get_args();
if (isset($arguments[1]) && is_string($arguments[1])) {
return $factory->of($arguments[0], $arguments[1])->times($arguments[2] ? null); // specifically, this is line 500
} elseif (isset($arguments[1])) {
return $factory->of($arguments[0])->times($arguments[1]);
}
return $factory->of($arguments[0]);
}
Please change as per below code,
$arguments[2] ? null TO $arguments[2] ?? null
The problem was that I just uninstalled xampp and it downgraded my locally running php version, hence the artisan serve didn't know how to handle that.

Lumen call to DB::connection() returns null even though select() is successful

I am using Lumen 5.3.1. $app->withFacades() and $app->withEloquent() have been uncommented in app.php. In web.php I run the following code:
$app->get('foo', function () {
return app('db')->select("SELECT * FROM foo");
return "Connected successfully to database " . DB::connection()->getDatabaseName();
});
The select() call correctly returns data from the foo table. However, DB::connection() returns:
FatalErrorException in Manager.php line 74:
Call to a member function getConnection() on null
Why does one work but not the other?
I'd say double check your service providers. It looks like you are going through the DB Capsule, when actually that's intended for use out of Laravel/Lumen. Anyway if you are in fact using the Capsule Manager, you probably have to register it in a boot method of the provider, not register.
Also, in order to find out more about what's going on, add this to your test code:
dd(app('db'), DB::getFacadeRoot());
Share the result if you want, this will give more information about the difference between the two methods.
app('db')->select("SELECT * FROM foo");
DB::connection()->getDatabaseName();
try
app('db')->connection()->getDatabaseName();
or
\DB::connection()->getDatabaseName();

Laravel artisan route:list shows non-object Exception

I am new to laravel. Recently I cloned sample project from github. When I try to execute php artisan route:list shows
PHP Fatal error: Call to a member function getMemberType() on a non-object in /...app/Http/Controllers/Admin/BaseAdminController.php on line 91
[Symfony\Component\Debug\Exception\FatalErrorException]
Call to a member function getMemberType() on a non-object
BaseAdminController.php
public function __construct(EmployeeDetails $employeeDetails)
{
$this->middleware('auth');
if(Auth::user()->getMemberType() != 'employee') //Line 91
{
Auth::logout();
return Redirect::to('secure/login');
}
When you fire artisan task, user object is not set, so \Auth::user() returns null and you see this error.
That's why you have to check if your app is running in console. You can do it via \App::runningInConsole() method.

Laravel 5 blade shows a blank page when there is error instead of throwing exception

In laravel 4 when you try to render a view that does not exists in app\views or a view with undefined variables laravel will throw an exception or show error that helps with debug.
I have a fresh installation of laravel 5.0.13 and am having a tough time troubleshooting a blade template that shows a blank page when i render a view that does not exists instead or a template with undefined variables instead of throwing an exception or error which will clue me in when debug.
I have installed filp/whoops:~1.0. but still recieve a blank page
class ProfileController extends Controller
{
public function index()
{
return view('indexx'); //this view does not really exist
}
}
The file indexx does not exist in my resources/views and i expect Laravel to throw an exception but am getting a blank page instead, why?
Also when i render a view that exists with some undefined variables i simply get a blank page
Example:
class ProfileController extends Controller
{
public function index()
{
return view('index'); //this view exists
}
}
The content of resources/views/index
{!! $this_variable_was_not_passed_an_I_expect_error !!}
As you can see in the view file above the variable does not exist by laravel will simply show a blank page instead of throwing an exception or some debug error.
Also to note i change my laravel default view in config/views
'paths' => [
//realpath(base_path('resources/views'))
realpath(base_path('resources/themes/default'))
],
And laravel was able to render views from resources/themes/default as long as there is no error in the template however, if any error was encountered such ar undefined variable a laravel displays a blank page instead of showing error message
Also to mention that I install virtual box and vagrant on window 7
Could this be a bug or something? Please assist.
Try to change permission on the storage folder:
chmod -R 0777 storage
It worked for me.
I don't really know why this happened but its now working as required after i run
vagrant destroy to destroy homestead VM
and then vagrant up - to create the VM
The error messages has now showing up instead of blank page:
An alternative solution which helped me, is running
composer install
I deployed with git which auto ignores some files. running composer install fixed it for me.
I got the same problem but here comes the reason and the solution:
The logfiles in storage/logs needs to be owned/writable by the webserver user. Otherwise L5 gives you a blank page.
I run "php artisan ..." always as root. If an exception was thrown and Laravel creates a new logfile, then it's owned by root. On my Debian i run in the project root folder:
chown www-data.root * -R
to solve this problem.
//lavarel 4
* Add this to app/start/gobal.php
App::error(function(Exception $e, $code) {
$runner = new \Whoops\Run;
$inspect = new \Whoops\Exception\Inspector($e);
$handler = new \Whoops\Handler\PrettyPageHandler;
$handler->setRun($run);
$handler->setInspector($insp);
$handler->setException($e);
return $handler->handle($e);
});
//lavarel 5
* Add this to app/Exceptions/Handler.php
public function render($request, Exception $exception)
{
..........
$runner = new \Whoops\Run;
$inspect = new \Whoops\Exception\Inspector($e);
$handler = new \Whoops\Handler\PrettyPageHandler;
$handler->setRun($run);
$handler->setInspector($insp);
$handler->setException($e);
$handler->handle($e);
......
return parent::render($request, $e);
}

Laravel 5 testing undefined property $client

I'm giving Laravel 5 (4.3) a go and have tried writing some simple controller tests like so:
public function testIndex()
{
$this->call('GET', 'posts');
$this->assertResponseOk();
}
When I run the test I get an error:
ErrorException: Undefined property: PostsControllerTest::$client
Why has the client attribute disappeared from the test class in Laravel 5?
Functions like
$this->assertResponseOk();
are not available in the current build of Laravel 5. I'm not sure if Taylor plans to reintroduce them.
I think you can add the following package to get back some of the functionality
https://github.com/orchestral/testbench

Categories