4 Replies - 251 Views - Last Post: 25 May 2020 - 08:24 AM Rate Topic: -----

#1 justawebuser   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 83
  • Joined: 09-February 14

Use Laravel features outside of it

Posted 24 May 2020 - 10:48 AM

Assume I have test.php file inside public folder. I want to read a table and dump it.
    require "../vendor/autoload.php";
    
    use Illuminate\Support\Facades\DB;
    use Illuminate\Support\Str;
    
   
    $links = DB::select('select * from tbl_links ' );
    dd($links); 


Now I go to mydomain.ir/load.php .

Quote

> Fatal error: Uncaught RuntimeException: A facade root has not been
> set. in
> /home/dastanez/zanganli.ir/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:258
> Stack trace: #0 /home/dastanez/zanganli.ir/public/load.php(8):
> Illuminate\Support\Facades\Facade::__callStatic('select', Array) #1
> {main} thrown in
> /home/dastanez/zanganli.ir/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php
> on line 258


How can I fix this? Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Use Laravel features outside of it

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3835
  • View blog
  • Posts: 13,977
  • Joined: 08-August 08

Re: Use Laravel features outside of it

Posted 24 May 2020 - 11:12 AM

Why would you want to run php from a public folder?

I'm guessing you want to run a command while developing. If that's the case, why not build a command that does what you want?
https://www.phpflow....d-in-laravel-5/
Was This Post Helpful? 1
  • +
  • -

#3 Ornstein   User is offline

  • D.I.C Head

Reputation: 103
  • View blog
  • Posts: 209
  • Joined: 13-May 15

Re: Use Laravel features outside of it

Posted 24 May 2020 - 12:06 PM

Do you understand how facades work within Laravel, the purpose they serve, etc? That might be something to brush up on if you're planning to go down this route.

The error you're getting is essentially because the DB facade class doesn't do anything by itself beyond enabling you to perform static method calls on another ("root") DB class/object - and in this instance, you've not set/created that object or established any way for the facade to resolve the class itself.

... So you'd usually need to create an appropriate class yourself; Illuminate\Database\DatabaseManager is probably what you're expecting / what you're familiar with in this instance, but also keep in mind that DatabaseManager will have some requirements of its own - so you'd have to go down that rabbit hole, too.

If you check out the GitHub page for illuminate/database, you'll see an (easier) example of how to setup/use this package by itself.

From here, there's a few different approaches you could take. You can either use the non-static methods in conjunction with the $capsule instance - or use the static methods that the Capsule class enables (as per the GitHub examples) - or I'm 90% sure you can hack the DB facade to work by doing something like:

DB::swap($capsule->getDatabaseManager());

This post has been edited by Ornstein: 24 May 2020 - 12:07 PM

Was This Post Helpful? 1
  • +
  • -

#4 justawebuser   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 83
  • Joined: 09-February 14

Re: Use Laravel features outside of it

Posted 25 May 2020 - 05:55 AM

View PostCTphpnwb, on 24 May 2020 - 11:12 AM, said:

Why would you want to run php from a public folder?


I'm using a limited shared hosting and I'm trying to run the following cmd via cronjobs as document says:
cd /home/dastanez/zanganli.ir && php artisan schedule:run >> /home/dastanez/report.txt 2>&1 

It fails and according to report.txt :

Quote

proc_close() has been disabled for security reasons


Most of the time contacting with administrators doesn't help. Whatever I say they would say you should buy VPS.
So I came to conclusion to create a crob/job.php file and autoload everything there and write my codes there.
That's why I asked this question.

Quote

Do you understand how facades work within Laravel, the purpose they serve, etc? ..

Actually no. Thank you. I will try it.

Thank you both

This post has been edited by justawebuser: 25 May 2020 - 06:02 AM

Was This Post Helpful? 0
  • +
  • -

#5 Ornstein   User is offline

  • D.I.C Head

Reputation: 103
  • View blog
  • Posts: 209
  • Joined: 13-May 15

Re: Use Laravel features outside of it

Posted 25 May 2020 - 08:24 AM

As it stands, I think the only reason Artisan needs to open a process is to run stty - which it shouldn't need to do if you were to set the LINES and COLUMNS environment variables yourself. There's a few different ways you could approach this, but as a quick test you might try opening the "artisan" PHP file (the one in the root directory) and putting this near the top, after the opening PHP tag:

putenv('LINES=25');
putenv('COLUMNS=80');


If that doesn't work, I/you could probably dream up a few other ways to run Artisan commands on your own terms.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1