Laravel - Non-static method should not be called statically - laravel

I have recently started using laravel for work and I boumped into many issues I cannot stil solve.
I have looked over many many topics and already anwered questions but none of those have helped me with my issue
So, I get this error trying to do 'php artisan serve'
"Non-static method Illuminate\Cache\RateLimiter::for() should not be called statically"
So I went up looking at the code, this is the RateLimiter.php code that gives me the error
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
});
}
the error is at the 2nd line of this code, in the RateLimiter:: etc
I get those errors in the CMD
app/Providers/RouteServiceProvider.php:59
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Non-static method Illuminate\Cache\RateLimiter::for() should not be called statically", "myPathToTheProject/app/Providers/RouteServiceProvider.php", [])
app/Providers/RouteServiceProvider.php:38
App\Providers\RouteServiceProvider::configureRateLimiting()
the function is called like this
$this->configureRateLimiting();
hoping you can help me, I will give more infos if are needed

I found out the problem. You need to import the facade, and when you auto-import, it tends to get the wrong package.
Look at the imports. If you find:
use Illuminate\Cache\RateLimiter;
You should replace with
use Illuminate\Support\Facades\RateLimiter;
Worked for me!

Related

Laravel 5.8 Call to undefined method Illuminate\Events\Dispatcher::fire()

I get this error on user registration. I have searched for this problem a lot and still couldn't solve the problem on my side. In laravel 5.8 upgrade, it's written that function fire() is changed to dispatch(), but I can't find any fire() function in any file of my app, so I can see what's happening.
Would appreciate any help. Thanks.
From: https://laravel.com/docs/5.8/events#dispatching-events
Instead of:
Event::fire(new \App\Events\NewUserSignup($new_user));
Do instead:
event(new \App\Events\NewUserSignup($new_user));
The fire method (which was deprecated in Laravel 5.4) of the Illuminate\Events\Dispatcher class has been removed(https://github.com/laravel/framework/pull/26392). You should use the dispatch method instead.
Instead of:
Event::dispatch('customer.created', $customer);
Do this:
Event::dispatch('customer.created', $customer);
OR:
event('customer.created', $customer);

View::make in Phpunit

I've a function that returns a View::make($string). I want to test that this function did indeed return an instance of View object. $string points to a file that does exist.
When I try to run this function within Phpunit it doesn't seem to finish. How can I test in Phpunit that a View object was created?
Laravel has helper methods specifically designed for testing views.
Some of them include:
$response = $this->get('/path/to-your-route');
$response->assertViewIs($value);
$response->assertViewHas($key, $value = null);
$response->assertViewHasAll(array $data);
$response->assertViewMissing($key);
More info can be found here: https://laravel.com/docs/5.5/http-tests#available-assertions
If you need to assert that something is an instance of something else, you can try the following:
$this->assertInstanceOf($expected, $actual);
When you provide invalid string the view object will not be created and will throw an exception. Not sure what you have in your function that prevents the exception, but the way to go around this issue, is to include this line in the failing test:
$this->expectException(InvalidArgumentException::class);
The issue stemmed down from usage of var_dump as I wanted to see the object in question. As nothing was presented in output, I assumed that had to do with View::make rather than outputting the object to the console.

Trying to get property of non-object in codeigniter class my_exception

I encounter this error in my CI code
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: core/MY_Exceptions.php
Line Number: 11
and this is my CI code
class MY_Exceptions extends CI_Exceptions {
function __construct() {
parent::__construct();
$CI =& get_instance();
var_dump($CI);
}
I don't know why this happen, wherein in the documentation it says the same syntax.
Is there anyone here know about this?
What to do about it?
New to me, but I found this and this might help
https://forum.codeigniter.com/thread-67859-post-342719.html
When extending the CI Exception class, there is no way to get the
instance with "get_instance()" because as what i've seen, CI isn't
loading the core before the Exception controller. I need some basic
functions to generate my 404 error pages, so this behavious is
completely useless for me as it only handles static output.

Migrating to Laravel 5, route:cache throwing exception

Moving from Laravel 4.2 to 5.0
We have about ~200 routes in our API, most of them were Closures. So I've spent the better part of the last couple days converting them all to controller routes. Everything is back to being stable, but when I try to run php artisan route:cache it throws an exception
[Exception]
Serialization of 'Closure' is not allowed
I did a ctrl+f for function, and the only ones left in the routes file are the two Route::group entries (which, yes, are closures...but surely only routes count...).
Any guidance? After the tediousness of the last two days, this error is...not what I wanted to see.
EDIT: Full stacktrace
Route cache cleared!
[Exception]
Serialization of 'Closure' is not allowed
Exception trace:
() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\laravel\framework\src\Illuminate\Foundation\Console\RouteCacheCommand.php:95
serialize() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\laravel\framework\src\Illuminate\Foundation\Console\RouteCacheCommand.php:95
Illuminate\Foundation\Console\RouteCacheCommand->buildRouteCacheFile() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\laravel\framework\src\Illuminate\Foundation\Console\RouteCacheCommand.php:65
Illuminate\Foundation\Console\RouteCacheCommand->fire() at n/a:n/a
call_user_func_array() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\laravel\framework\src\Illuminate\Container\Container.php:523
Illuminate\Container\Container->call() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\laravel\framework\src\Illuminate\Console\Command.php:115
Illuminate\Console\Command->execute() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\symfony\console\Symfony\Component\Console\Command\Command.php:257
Symfony\Component\Console\Command\Command->run() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\laravel\framework\src\Illuminate\Console\Command.php:101
Illuminate\Console\Command->run() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\symfony\console\Symfony\Component\Console\Application.php:874
Symfony\Component\Console\Application->doRunCommand() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\symfony\console\Symfony\Component\Console\Application.php:195
Symfony\Component\Console\Application->doRun() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\symfony\console\Symfony\Component\Console\Application.php:126
Symfony\Component\Console\Application->run() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php:94
Illuminate\Foundation\Console\Kernel->handle() at C:\Users\cjtho_000\Desktop\ecapi_l5\artisan:36
EDIT 2: removed the route groups to test, still throws the exception
Turns out, the entirely unhelpful error message was trying to tell me that I had a duplicate route (copy paste error, I had two get routes matching the same uri).
Removing the duplicate fixed the issue.
Per your stack trace,
vendor\laravel\framework\src\Illuminate\Foundation\Console\RouteCacheCommand.php:95
serialize() at vendor\laravel\framework\src\Illuminate\Foundation\Console\RouteCacheCommand.php:95
Laravel is trying to serialize the routes collection on line 95 of RouteCacheCommand.php.
The error you're getting
Serialization of 'Closure' is not allowed
looks like its PHP's standard "someone tried to serialize an anonymous function" exception message (vs. it might be a custom Laravel exception).
All this means something is adding an anonymous function (i.e. "closure") to your route collection. If you're 100% sure you've removed them all from routes.php, my guess would be the route's in third party code somewhere.
It's best not to guess. Try some debugging code that's something like the following
//un-tested pseudo debugging code
//get the application's routes, using the same
//technique as the route:cache command
$app = require /path/to/your/laravel/bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
$routes = $app['router']->getRoutes();
//go through each route
foreach($routes as $route)
{
//if the route is a closure
if(is_object($route) && ($route instanceof Closure))
{
//use reflection to find out which file the closure is defined in
$r = new ReflectionFunction($route);
var_dump($route->getFileName ());
}
}
This will let you figure out where the closure that's causing your problems is defined, which should be enough to point to the problem.

Queued Events in Laravel 4: Event::flusher() method not found

According to the Laravel 4 Documentation on queued Events, I tried to register an event flusher this way:
Event::flusher('foo.bar', function($data)
{
Mail::send(array('emails.notification', 'emails.notification_text'), array('content' => $data), function($message)
{
$message
->to('email#example.com', 'My Name')
->bcc('test#example.com')
->subject('Message from Listener');
});
});
But I am getting the following error upon loading of the script:
Call to undefined method Illuminate\Events\Dispatcher::flusher()
I also couldn't find this method in the source codes of L4. But when I change this from Event::flusher() to Event::listen(), everything works as expected.
So my guess is, that the documentation isn't up to date and the Event::flusher() method has been dropped, since Event::listen() does the same work. Or are there any differences between those two methods and I have an error in my code?
You may need to update your libraries using:
$ composer update
If that doesn't work, let us know what your composer.json file looks like - you might be using a beta version if the framework. It was updated very often before the first stable release.

Resources