1

New to PHP. Trying to implement a simple Slack integration using Laravel. Successfully added alek13/slack following instructions here and confirmed it's in my project's composer.json file. According to the guidance:


Quick Tour

  • create an incoming webhook & copy hook_url
  • composer require alek13/slack
  • add the following code
use Maknz\Slack\Client;
require(__DIR__ .'/vendor/autoload.php');

$client = new Client('https://hook_url');
$client->to('#general')->send('Good morning');

Done!


While this may be obvious to PHP veterans, I am unable to gather from the documentation any reference as to exactly into which file the sample code above should be inserted. (e.g., ./vendor/autoload.php? ./config/...?, etc.)

Any guidance is appreciated!

Jasper33
  • 519
  • 3
  • 6
  • 18

1 Answers1

1

This answer sheds some light on exactly how Laravel requires vendor/autoload.php. (It's required in bootstrap/autoload.php).

That being the case, you can just use Maknz\Slack\Client in the PHP file you need the Client class in, and initialize it there. (Or, consider a dependency injection pattern if multiple files will require the same instance.)

(Under the hood, Composer is using spl_autoload_register to register its own ClassLoader class, in case you're interested in more of the guts of the process.)

Cameron Hurd
  • 4,836
  • 1
  • 22
  • 31
  • 1
    Many thanks for the references - going through them now to see if I can find a suitable solution. Will do housekeeping on the Answer if/when I solve it. Very much appreciated! – Jasper33 Jan 26 '22 at 09:24
  • 1
    @Cameron_hurd Thank you for the pointer - following these threads helped me down the path. Scope of functions is a bit different for me (especially being new to PHP) but ultimately not too different from other languages. Found what I needed following "ClassLoader" paradigm. Many thanks! – Jasper33 Feb 16 '22 at 03:47
  • 1
    Followed links on this topic until I ended up here - this solved my particular problem and answered my question: https://stackoverflow.com/a/18075712/1244159 – Jasper33 Feb 16 '22 at 04:21
  • 1
    In the end, I skipped the Slack module altogether and built a simple function using the build-in CURL module. – Jasper33 Feb 16 '22 at 04:23
  • Glad to hear you got it solved! FWIW, I've never gone the route detailed in the answer you linked above. It seems like it would be hard to keep track of / rely on and rather nightmarish to deploy any way but "by hand". – Cameron Hurd Feb 17 '22 at 06:20
  • Agreed, but this was a "one-off" to find an "easy" way to pull in some oft-used services for debugging (dumps, Slack messages, other various `curl` stuff). If time were a luxury, I would have followed a more elegant path. NOT RECOMMENDED as a solution for proper and long-term projects!! (still, thanks for the pointers!) – Jasper33 Feb 17 '22 at 07:08
  • 1
    all power to ya – Cameron Hurd Feb 18 '22 at 02:45