Is it possibile using Artisan command on web server? - laravel

I would like to ask if is possible using following command on webserver.
public function addCache() {
\Artisan::call('config:cache');
}

Yes it is possible, connect to your web server with terminal, most of the time you will need to ssh into it, and then navigate to the project root folder, then run the php artisan command as what you typically do

You can create a route with artisan call
Route::get('/clear', function () {
Artisan::call('config:clear');
});

If you don't have terminal on your host or server, you can define a route and define this artisan call inside that.
Route::get('cache-config',function(){
\Artisan::call('config:cache');
return back();
});
Or you can use this package for this artisan commands and some other useful commands.

Related

How to link public storage without ssh access or cron jobs?

I need to deploy a laravel app, which uses the storage folder as public storage but I don't have ssh access. I'm trying to find a substitute for:
php artisan storage:link
I've found this thread: Generating a link by putting this route into web.php:
Route::get('generate', function (){ \Illuminate\Support\Facades\Artisan::call('storage:link'); echo 'ok'; });
But it didn't solve the problem. Am I doing it wrong? How do I link public storage?
Edit: The shitty server I'm trying to deploy to doesn't offer cron jobs.
create a corn job if available and after the job run the first time. delete this
ln -s /home/user/laravel/storage/app/public /home/user/public_html/storage
The problem was that I didn't understand how to use the storage link route correctly. I had to add:
Route::get('/linkstorage', function (){
\Illuminate\Support\Facades\Artisan::call('storage:link');
echo 'storage linked!';
});
to web.php and actually navigate to domain-name/linkstorage to trigger the artisan call. After that the storage was linked and everything worked.

Seeders in this scenario

I have an email configured in the services.admin.key that has the email of the admin of the app.
The app only has one admin. Do you know the best approach so that someone who has access to the code from Git could run a command to configure the user? For example, if its a table with three columns:
name: admin
email: the email configured in`services.admin.key`
password: sometestpass
How can I allow someone who has access to the project to run some command to create one user with these details? Do you know what the best approach for this is? I doubt how to approach this properly if it should be with a seeder or another approach?
For example, I have this method on the user model that I use in some views to check if the current user is an admin.
public function isAdministrator ()
{
if ($this->email == config('services.admin.key'))
{
return true;
}
return false;
}
If you talk about fresh project on local database, it would be nice to get ready seeder for admin user.(not remote database). let's say I got access to git repo I could clone project, and run composer install for dependencies and than PHP command php artisan migrate --seed it would be good to have seeder ready for creating admin. To make seeder you need just php artisan make:seed AdminSeeder
and after write in AdminSeeder file something like:
User::create(['name' => 'admin', 'email' => config('services.admin.key')),'password' => bcrypt('sometestpass'),]);
than in Database/seeders/DatabaseSeeder.php write $this->call(AdminSeeder::class);
it's all. now when someone get fresh project it does 3 commands to make everything ready.
composer install
php artisan migrate --seed
php artisan key:generate
hope I understood right way what you needed.

How to migrate tables on a running site for laravel?

Using laravel how to migrate table(if there are changes) on a running site?
We use CMD if working offline. My question is simple if I have a site running fine already (online) then how to migrate...we can't run CMD there, right?
one way is using Artisan command in routes like this:
Route::get('/migrate', function(){
\Artisan::call('migrate');
dd('migrated!');
});
and then call this route.
remember to REMOVE this route after MIGRATING...!
if your server is linux you can use ssh to access command environment.
If your personal system is Windows you can install putty to open ssh.
how it works is in this link :
https://mediatemple.net/community/products/dv/204404604/using-ssh-in-putty-(windows)
how to access ssh in mac system is in this link
https://www.servermania.com/kb/articles/ssh-mac/
after access ssh you most first install composer then locate your project folder and now you can call all php artisan command like
php artisan migrate
import Artisan at web.php
use Illuminate\Support\Facades\Artisan;
you can write migrate route like this
Route::get('migrate',function(){
Artisan::call('migrate');
});
you can write migrate:rollback route like this
Route::get('rollback',function(){
Artisan::call('migrate:rollback');
});
you can write routes & view clearing route like this
Route::get('vr-clear',function(){
Artisan::call('route:clear');
Artisan::call('view:clear');
});
Basically i will suggest you too use like this reboot route for clearing cache and route clear after deploying the Laravel project to live.
Because sometimes it can be show some error message.
Route::get('reboot',function(){
Artisan::call('view:clear');
Artisan::call('route:clear');
Artisan::call('config:clear');
Artisan::call('cache:clear');
Artisan::call('key:generate');
});
The answer is also simple. You're still going to use CLI or CMD while your site is online. If you use cPanel, go to Advanced tab and select terminal. Inside your terminal use cd to change drive. Default drive might probably be public_html. cd public_html, cd <your_project_folder>php artisan migrate.
This will process migration while your application is running.

Symbolic link not working in Laravel and image not loading

My problem is that when I upload the archim with my site to the hosting and then unpack it there, I lose the symbolic link and the images stop working. How can this be fixed? Everything is fine on the local computer, and I use php artisan storage:link, but there is no terminal on the hosting.
If you have no ssh/terminal access, then create symbolic link with Route, run it once & delete it.
route/web.php :
Route::get('/sym', function () {
Artisan::call('storage:link');
});
Hope this helps
Well, if you have ssh access to the hosting, execute the command.
If you don't have ssh access, like basic hosting with cpanel, you need to configure a cronjob to execute the command. Example:
* * * * * /path/to/php /home/path/to/project/php artisan storage:link

How to run Laravel artisan on a live server without shell_exec?

My new project is based on Laravel framework, and developers need to run php artisan commands to do their requirements. On the other hand, server configuration disabled shell_exec for security purposes.
What should I do for this case? Is there any secure way for developers considering server security issues?
There are three ways to run php artisan command.Below i have mention all of them :
1) Use Terminal which is on server.Through that you can execute the php artisan command.
2) Use "putty" software to access the server.In that you can connect putty with server.For connection to the server you require "SSH credential" which you can create on server.
3) Using route you can execute artisan command.But for that every time you need to add/modify route and execute that in the browser so that artisan command will execute.Below i have given an example of it.You just have to put it in the routes file:
Route::get('command', function () {
/* php artisan migrate */
\Artisan::call('migrate');
dd("Done");
});
The above route you need to call through browser.This route will execute command of "php artisan migrate".

Resources