How can i run artisan commands in cpanel
Asked Answered
D

4

7

How can i run these artisan commands in my application that is hosted in the net? Is there like a cmd in my cpanel where i can do these commands? Thanks in advance.

  • php artisan clear:cache
  • php artisan view:clear
Date answered 8/12, 2017 at 9:16 Comment(2)
How about a SSH access?Funereal
make sure to paste the URL to your site here when you are done, so we can all clear your cachesSprat
K
8

Now in Laravel 5.8, you cannot pass object to call() func. You must pass an array [] as second argument to call() func.

Route::get('/clear-cache', function() {
    $output = [];
    \Artisan::call('cache:clear', $output);
    dd($output);
});
Kidder answered 29/7, 2019 at 3:48 Comment(0)
E
4

You can make a personalized route, and call it when you need it:

Route::get('/clear-cache', function() {
    $output = new \Symfony\Component\Console\Output\BufferedOutput;
    \Artisan::call('cache:clear', $output);
    dd($output->fetch());
});

Another solution is to access ssh to your server and to run the commands.

Evident answered 8/12, 2017 at 9:17 Comment(4)
I tried the personalized route. How will i know if the cache was successfully cleared?Date
You can add an echo for your output. Look to the update of the answer.Evident
(1/1) FatalErrorException Class 'BufferedOutput' not found - i get this error.Date
you have to add the full path for it : \Symfony\Component\Console\Output\BufferedOutputEvident
M
4

Try this. You can clear all of laravel application cache hosted in shared hosting server that can not access ssh shell by the following code:

Route::get('/cleareverything', function () {
    $clearcache = Artisan::call('cache:clear');
    echo "Cache cleared<br>";

    $clearview = Artisan::call('view:clear');
    echo "View cleared<br>";

    $clearconfig = Artisan::call('config:cache');
    echo "Config cleared<br>";

    $cleardebugbar = Artisan::call('debugbar:clear');
    echo "Debug Bar cleared<br>";
});

Now run yourdoamin.com/cleareverything

This code does not throw any error. I already used this code.

Ref : https://laravel.com/docs/5.2/artisan#calling-commands-via-code

Ms answered 8/12, 2017 at 9:52 Comment(5)
Oh no !!!! totally wrong .... you just assume that the cache was successfully but you are not sure, it can also fail !!!Evident
I already used this code in my various application and successfully clear cache and I have confirmed you that there is no error in this codeMs
There is non sense people who give down vote without testing code. I have 3 years experience in laravel application development and I used this code in many projects.Ms
I respect your experience, but sorry to say it, this code is wrong ! you ensure that the command was done corretly, with using echo, when you really don't know the output from it ...Evident
Yes I agree with you, but you show cache cleared, without to know if it was really working this command or not ! Sometimes it can return an error, but with you code it will just output a success !!Evident
V
2

You could create a simple bash script called clear-cache.sh like this:

#!/bin/sh
PHP=/path/to/your/php-binary
PATH=/path/to/your-artisan-install

cd $PATH
$PHP artisan clear:cache
$PHP artisan view:clear

Save the script and make it executable (chmod +x clear-cache.sh). Run it through a cronjob at specific intervals and configure the cron job to email you the output of those 2 commands. This way you'll get an email, every time the cron runs the script (basically the cron will automatically issue your two commands) and the ouput will be emailed to you.

Of course there are other methods as well like creating a php script and invoke it via web

Vestment answered 9/12, 2017 at 9:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.