vendredi 4 décembre 2020

Use existing Laravel application's namespace in independent script

I created a script app/notify_uploaded_doc.php, which needs to use some existing Laravel Models and Mailables (defined in app/ as well, and namespaced under MyApp). When I tried to include them in the script with use MyApp\RequiredDocument; for example, and execute the script on the command line with $ php notify_uploaded_doc.php, it gives me the exception: PHP Fatal error: Uncaught Error: Class 'MyApp\RequiredDocument' not found in /home/jyscao/myapp/app/notify_uploaded_doc

So then instead of use, I try require dirname(__FILE__).'/RequiredDocument.php'; instead, and this does import the predefined model; but the problem then shifts to the imports from the model file itself not being sourced properly. With the error now becoming: PHP Fatal error: Uncaught Error: Class 'Illuminate\Database\Eloquent\Model' not found in /home/jyscao/myapp/app/RequiredDocument.php

So what's the correct way to do PHP imports in this situation?



Additional Context

If it helps, the concrete problem I am trying to solve is this:

I'm adding a notification system to this existing Laravel CRUD app. In the app, users are sometimes required to upload PDF documents, where admins (each managing several users) can then review the docs and take further action (approve, reject, etc.). Currently there is no systematic way for admins to know when users under their management have uploaded documents, and so must manually check periodically by logging into the app.

My approach right now is to create a separate PHP script that does a DB query for newly uploaded docs from users that admins have yet been notified of (this info is stored in a newly added column). Do a bit more processing, such as grouping all docs by uploader, and all uploaders by the admin who manages them, then send batch emails to admins. My thinking is that this script once complete, can be run as a cron job say every 15 or 30 min, hence why I placed its code into its own separate script.

I don't necessarily have to stick with this cronjob script implementation, it's simply what I came up with. So if you think there are better approaches, please do tell.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire