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

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.

Related

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.

Laravel Artisan & imap_open()

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)

Laravel: Base table or view not found: 1146 Table 'database.pages doesn't exist

I'm working on a CMS and I have a little problem with my migrations. I added a new migration file and I wanted to add that one. That didn't work so I ran this bit:
php artisan migrate:reset
After that I ran this bit:
php artisan migrate:install
php artisan migrate
And now I get this error:
{"error":{"type":"Illuminate\\Database\\QueryException","message":"SQLSTATE[42S02]: Base table or
view not found:1146 Table 'cms.pages' doesn't exist (SQL: select * from `pages`)"
The error kinda tells me that it can't find the database, because that's true.
I also have a command that runs the migrate and I run that one like this:
php artisan app:install
But that shows the same error...
Remove any lines requesting data from your model from these files to be sure artisan is not trying to load data from your non-existent table:
bootstrap/start.php
app/start/global.php
app/start/local.php
app/routes.php
Also be sure to un-register any service providers that utilize data from that table in their register or boot methods inside of app/config/app.php.
The issue is that these files not only get executed for browser (web) requests, but for all requests, including command-line artisan invocations (e.g. php artisan migrate). So if you try to use something before it is available in any of these files, you are going to have a Bad Time.
You can use this to dictate when your app is running from the console.
I believe this issue only occurs when you run a command
if( !App::runningInConsole() ){
//allow laravel-menu to run
}
This way you will prevent data load from your non-existent table

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