Laravel Artisan & imap_open() - php

Currently experiencing a weird bug or something I might be overseeing. (Laravel framework)
I have a plain HomeController where I am doing a succesful imap_open() connection. ( I can read out the complete mailbox using the browser )
But once I transport this function to an artisan command, it returns me:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Call to undefined function App\Console\Commands\imap_open()
What am I missing or is this just plain not possible?

You try to be able to call it though Artisan, but you need to add an Artisan function since Artisan can't know what you want to do, please check the documentation section Writing Commands here.
For now you're calling an unknown Artisan function that's why you get this

Your code is currently in the App\Console\Command namespace.
Because you are calling the imap_open function without specifying the namespace, it is looking for a App\Console\Command\imap_open function and can't find it.
Because it's a default php function, the key here is to call it in the global namespace using a \
So make sure you call it like that:
$res = \imap_open(...params);
Also make sure the imap is compiled/enabled for the php cli (as the php.ini for cli is different from the web one)

Related

Getting error for artisan commands ( In routes-v7.php Call to undefined method Closure::__set_state() )

I have upgraded Laravel version from 5.7 to 8.34. I'm getting this error for all artisan commands.
To others: removing the routes-v7 might help, but it's possibly caused due to a return in your routes file (web.php). Remove it and use a controller to return a specific view/back() etc.
Call to undefined method Closure::__set_state()
In my case, I delete the bootstrap/cache/config.php file and run the PHP artisan vendor: publish but the same error occurs in the terminal.
Solution:-
1 Delete the routes-v7.php under the bootstrap folder path(bootstrap/cache/routes-v7.php) .
2 Run the PHP artisan optimize Command. then they recreate the deleted file in the repository.
Laravel 8 && Laravel 9
Please Support !!!!
https://stackoverflow.com/users/16749364/hitesh-sharma
#Tjab answer is the correct one. Just a few more thoughts:
It's because you're using Closures (a.k.a. Lambda functions function () use() {}) in your configuration. I'm pretty sure it's because you're using HTTP redirect routes. DON'T USE THAT FEATURE, since you won't be able to cache your routes, which is actually a good idea because it should increase your application speed. Otherwise your config can't be serialized (what caching actually means) to disk.
Instead create controller and action for each redirect.
Also check your configuration files for Closures and remove them (same for all other PHP frameworks).

Call to undefined function App\Providers\studly_case() after upgrade to Laravel 6.0.x

As I mentioned in the question, I just upgraded the Laravel version of my project from 5.8.* to 6.0.*.
Now I am getting the below error:
In AppServiceProvider.php line 32:
Call to undefined function App\Providers\studly_case()
Is Laravel supposed to throw error in it's own core file ? I was invoking the following command when I got this error, so I suppose this happens with any artisan command:
php artisan list
I saw in docs, that the new function is Str::studly from Str class file. Is it ok to change in the AppServiceProvider.php ?
Referring to the Laravel 6.x docs, I just discovered that the function for converting the string's current case to studly case is moved to Illuminate\Support\Str class, and named as studly().
So I added that dependency and then replaced the studly_case() to Str::studly() in AppServiceProvider.php, and then was able to run the artisan commands just like before.
I wasn't sure that changing AppServiceProvider.php is standard way or not, but it did worked so I am posting the answer here, that way it'll help anyone else facing the same issue.

Calling Laravel Controller-function from within custom artisan-command -> unknown database

I've created a custom artisan-command in order to call a controller-function via commandline (without curl).
Since there seems to be no way in Laravel 4 to directly call the function I'm dispatching a request like this:
public function fire()
{
$request = Request::create('insert', 'GET', array());
Route::dispatch($request)->getContent();
}
When I call the command via command-line it starts showing the HTML-content the controller-function would output:
php artisan myApp:customCommand
<h1>Insert into Database</h1>
<pre>
but then returns this error:
[PDOException]
SQLSTATE[HY000] [1049] Unknown database 'appdatabase'
What can I do about it?
The reason there is no way to directly call the function is because you should never be calling a controller function from your artisan command.
You should refactor the controller's functions into its own class+function, then call that class+function from either your controller or your artisan command.
The problem here was a wrong database-configuration. For some reasons php artisan needs the port-number of the MySQL-server in the hostname of the database-configuration. The web-application itself only works without specifying the port number.
Since that is a more general problem, I created another question regarding that.
Artisan-commands need different hostname in db-config than Laravel itself
Thanks for the feedback regarding the web-application's structure.

Laravel 4 Commands stopped working suddenly

Back from vacation, and when I'm trying to run one of my previously working command:
php artisan list
Throws me:
[root#api2]# php artisan list
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Call to a member function getArgument() on a non-object","file":"\/var\/www\/html\/api2.alariscms.com\/vendor\/laravel\/framework\/src\/Illuminate\/Console\/Command.php","line":153}}
And I seriously cannot understand why this happens. I tried debugging the file that throws an error. It awaits argument:
InputInterface $input but it gets NULL
I don't know what can go wrong on a framework level to stop receiving the required object.
Any ideas where to start debugging from? Or I can reload configurations / update laravel via cmd?
Thanks
This is what I would do:
php artisan dump
composer dump-autoload
check for current Laravel version
and then go to that specific command and check for bug there.
Also, what kind of argument are you trying to pass to command ? Is that object - if so check where is implementation of that class ?

Laravel 4 testing "Command"s?

I've tried testing my commands in Laravel 4, as they are significant part of my system, but it seems like the documentation coverage is so poor, that they only explain basic testing of controllers and some models.
In Commands, you can pass arguments via command line to the class and it's received via $this->input property, something I don't know how to emulate.
Whenever I try to run the test for my command, when it expects an argument in "fire" method, I get this error:
Fatal error: Call to a member function getArgument() on a non-object in /var/www/html/project/vendor/laravel/framework/src/Illuminate/Console/Command.php on line 153
Which is logical, there's no argument passed. Is there a way to test this functionality?...
Thanks
Most of it can be done using Symfony Command Tester (since Command is based on Symfony Console), example: http://alexandre-salome.fr/blog/Test-your-commands-in-Symfony2. However this would start to fail if you have to call another artisan command such as $this->call('db:seed'); or etc because this is actually Illuminate\Console\Application specific syntax.
I'm all open if there anyone that have a solution for above scenario.
I have recently made a post about Testing Laravel commands.
If you have a specific part of code that you want to use in multiple commands then you have to move that part of the code somewhere where both commands can use it (Event handler, trait, another class ...) and then in your command reference that code instead of using $this->call('db:seed');

Categories