If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login SubscribeLet's do something fun! Google for SlackBundle - you'll find one called nexylan/slack-bundle
. This is a fun library that gives us a service that can send messages to a Slack channel. To install it, find the composer require
line, copy that, move over to your terminal and paste:
Tip
We need this for our course CI, just ignore this note and follow the tutorial without executing these commands :)
echo 'nexy_slack:' > config/packages/nexy_slack.yaml
echo " endpoint: 'https://hooks.slack.com/services/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'" >> config/packages/nexy_slack.yaml
composer require nexylan/slack-bundle:2.1.0 php-http/guzzle6-adapter:1.1.1
Interesting: this installs the bundle and some other library called guzzle6-adapter
. Wait for it to install and... it fails!
Don't panic. There are two important things happening. First, this installed two bundles! Cool! You can see both of them inside bundles.php
:
... lines 1 - 2 | |
return [ | |
... lines 4 - 11 | |
Http\HttplugBundle\HttplugBundle::class => ['all' => true], | |
Nexy\SlackBundle\NexySlackBundle::class => ['all' => true], | |
]; |
For the SlackBundle, it says "auto-generated recipe". That means that the bundle doesn't actually have a recipe... but Symfony Flex, at least added it to bundles.php
for us. By the way, this is not necessarily a bad thing: sometimes a bundle doesn't really need a custom recipe!
The second bundle did have a recipe. Before I started recording, I committed my changes so far. To see what that recipe did, let's run:
git status
Interesting: it added a new configuration file called httplug.yaml
:
httplug: | |
plugins: | |
redirect: | |
preserve_header: true | |
discovery: | |
client: 'auto' | |
clients: | |
app: | |
http_methods_client: true | |
plugins: | |
- 'httplug.plugin.content_length' | |
- 'httplug.plugin.redirect' |
We don't know what this does, but it probably configures some sensible defaults. Let's ignore it unless the docs tells us otherwise.
The second important thing I want to talk about is... well... this big error!
The child node "endpoint" at path "nexy_slack" must be configured.
Oof... this is not a great error message. It means that this bundle requires some configuration, which we don't have yet. And since it didn't add a configuration file via a recipe, we'll need to create it ourselves. But before we do that, the most important thing to understand is this: when you see an error like this after running composer require
, Composer did finish successfully and the library was installed.
Ok, let's go read the docs so we can figure out how to configure this bundle. Ah, so one of the reasons that installing this bundle isn't smoother is that its documentation is out-of-date! Hopefully it will be updated soon, but actually, this is a great example of how to navigate less-than-up-to-date docs.
How do I know it's out-of-date? This AppKernel
thing is a Symfony 3 concept. We don't need to worry about enabling bundles: this is done for us automatically.
If you scroll down... ah, here is the configuration. And it says that this is an example of default values... which probably means that we don't need to copy all of this. Yep, we just need to fill in the parts that are required, so, endpoint
.
Let's copy part of the configuration file. But... it doesn't tell us where to put this! That's ok! We already know: it can live in any file in config/packages
. Let's create a new one called nexy_slack.yaml
. Paste the config, but the only key we need is endpoint
:
nexy_slack: | |
... lines 2 - 3 |
If you're coding along, here's how this will work. First, you'll need access to a Slack workspace where you're an admin. If you don't have one, you can create one: it's free and easy.
Once you've got it, go to your domain /apps/manage
, then search the App Directory for "Incoming Webhooks". Click "Add Configuration" to setup a new webhook: I've already done this.
Thanks to this, you now have a new Webhook URL, which anyone can use to send messages to your Slack. There's no authentication - the URL is meant to be a secret. Um... yea, I know you can read mine - I'm super bad at secrets. I'll invalidate it after I record.
Copy the URL and paste it next to endpoint
:
nexy_slack: | |
endpoint: 'https://hooks.slack.com/services/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' |
Now, move over and clear your cache:
php bin/console cache:clear
Again, starting in Symfony 4.0.5, you will not need to clear your cache when adding a new config file.
Sweet! The bundle is configured, so... how do we use it? Go back to the docs. Below, yea! Usage! And this is where things get really interesting. The code says $this->get('nexy_slack.client)
. What the heck is that?
Actually, this is something from Symfony 3... which we do not recommend doing in Symfony 4 and may or may not work, depending on the situation. Basically, $this->get()
is, or was, a shortcut to fetch a service by its id. Instead of doing this, we are - of course - going to fetch the service via autowiring.
You guys know the drill: find your terminal and run:
php bin/console debug:autowiring
And search for "Slack". Wait... nothing!
Yep... this bundle technically works with Symfony 4... but it hasn't been fully updated. And so, it doesn't expose any services for autowiring! Right now, there is no way to autowire that nexy_slack.client
service.
We need to learn a little bit more about public versus private services. And then take control of things with an autowiring alias!
Hey Karin,
In their docs: https://github.com/nexylan/... I see the "endpoint" option is required and because it does not have a recipe that will put that config for you - it fails. Btw, do you have any configuration for "nexy_slack"? I'd recommend you to try to create a config/packages/nexy_slack.yaml first, paste there a correct "endpoint" URL and then try to install the bundle again with:
$ composer require nexylan/slack-bundle php-http/guzzle6-adapter:1.1.1
This should help, at least it should not revert composer.json. Let me know if it still does not work for you.
Cheers!
My nexy_slack.yaml was directly under config. I moved it to config/packages and now it works!
I was stuck on this soo long. Thanks.
Hey Karin,
Happy to hear it works for you now! You're welcome ;) But yeah, that bundle probably do need a recipe that will create that file for you. I even noticed an issue about it there: https://github.com/nexylan/...
Cheers!
I have created nexy_slack.yaml file in packages dir. then "composer require nexylan/slack-bundle php-http/guzzle6-adapter:1.1.1" and this is the message I got
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Installation request for php-http/guzzle6-adapter 1.1.1 -> satisfiable by php-http/guzzle6-adapter[v1.1.1].
- Conclusion: remove php-http/httplug 2.2.0
- Conclusion: don't install php-http/httplug 2.2.0
- php-http/guzzle6-adapter v1.1.1 requires php-http/httplug ^1.0 -> satisfiable by php-http/httplug[v1.0.0, v1.1.0].
- Can only install one of: php-http/httplug[v1.0.0, 2.2.0].
- Can only install one of: php-http/httplug[v1.1.0, 2.2.0].
- Installation request for php-http/httplug (locked at 2.2.0) -> satisfiable by php-http/httplug[2.2.0].
So After that I removed php-http/httplug 2.2.0 using composer remove php-http/httplug 2.2.0. As soon as I did that I got this message
php-http/httplug:2.2.0 is not required in your composer.json and has not been removed
Package "php-http/httplug:2.2.0" listed for update is not installed. Ignoring.
Loading composer repositories with package information
Updating dependencies (including require-dev)
Restricting packages listed in "symfony/symfony" to "5.1.*"
Nothing to install or update
Writing lock file
Generating optimized autoload files
composer/package-versions-deprecated: Generating version class...
composer/package-versions-deprecated: ...done generating version class
86 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
Executing script cache:clear [KO]
[KO]
Script cache:clear returned with error code 1
!!
!! In ArrayNode.php line 320:
!!
!! Unrecognized options "dir_name, namespace" under "doctrine_migrations". Ava
!! ilable options are "all_or_nothing", "check_database_platform", "connection
!! ", "custom_template", "em", "factories", "migrations", "migrations_paths",
!! "organize_migrations", "services", "storage".
!!
!!
!!
Script @auto-scripts was called via post-update-cmd
Question: how can I solve this issue, please? thank you
Hey Hicham A.!
Ah, wow! It looks like there is a lot going on here :). And actually, I think there are 2 separate issues.
1) First, the nexylan/slack-bundle problem
Due to changes in this library, exactly how you install it depends on which version you want. If you're following this tutorial exactly, then you should be able to run the command that you did. Well, almost - you didn't include the :2.2.0
part for the bundle:
composer require nexylan/slack-bundle:2.2.0 php-http/guzzle6-adapter:1.1.1
But you can also try to install the newest version of this bundle if you want, just via:
composer require nexylan/slack-bundle
But because that will give you v3 of the bundle, getting it working will be different than what we show in this tutorial.
2) Next, about the "Unrecognized options "dir_name,... " error
This is actually caused by doctrine/doctrine-migrations-bundle
somehow being upgraded from version 2.1 to 3. I didn't see how this happened in what you posted, but basically - it's complaining that your existing doctrine_migrations.yaml file is using the syntax config that's supported in version 1 and 2. The config format for version 3 is different.
The easiest solution is just to update your config in doctrine_migrations.yaml for the new format. You can compare the old and new format to see the changes needed:
Old format: https://github.com/symfony/recipes/blob/master/doctrine/doctrine-migrations-bundle/1.2/config/packages/doctrine_migrations.yaml
New format: https://github.com/symfony/recipes/blob/master/doctrine/doctrine-migrations-bundle/2.2/config/packages/doctrine_migrations.yaml
Let me know if that helps!
Cheers!
Hey! I get this error and I don't know how to fix this. Can somebody help me with this? :)
Circular reference detected for service "nexy_slack.client", path: "nexy_slack.client -> Nexy\Slack\Client -> nexy_slack.client".
Thank you
Hey @Anneleen Peeters
I guess you got newer version of nexylan/slack-bundle
so in next chapter there is a TIP that you should remove Nexy\Slack\Client: '@nexy_slack.client'
binding from config/services.yaml
file because it already configured by bundle!
Cheers!
for Symfony 4.4 you need to install these packages to make it work:
composer require nexylan/slack-bundle php-http/guzzle6-adapter nyholm/psr7
Hey Mykola M.!
Thanks for the comment :). Indeed, there is a bit going on with this library :/. Here are some details:
1) If you download the course code and follow exactly, you won't have a problem. The problem is introduced in nexylan/slack-bundle 2.2.1, which requires Symfony 4.2.12. This tutorial uses Symfony 4.0, so it won't be installed.
2) If you are running 4.2.12 or higher, you will get nexylan/slack-bundle 2.2.1 (or higher) which does appear to have some problems - specifically because that bundle requries nexylan/slack 3 (but 2.2.0 and lower require 2). The easiest way to fix this is to force version 2.2.0 of the bundle to be used in the tutorial:
composer require nexylan/slack-bundle:2.2.0 php-http/guzzle6-adapter:1.1.1
If you DO want to use the latest version of the bundle, I think you will need to install a few new packages - we actually talk about this exactly here: https://symfonycasts.com/screencast/symfony5-upgrade/lint-container#detecting-type-problems
In your case Nick, I'm not sure why also adding nyholm/psr7
solved your problem. Honestly, this bundle relies on something called httplug, which makes the dependency issues a bit hard to follow. Anyways, we have some suggestions on how to get the latest version of the bundle working - in case nyholm/psr7 doesn't work for others.
Cheers!
How am I able to remove a bundle ?
Didn't find anything about this, if i for example installed the wrong one
Hey John!
Sorry for my slow reply! If you installed the wrong bundle - e.g. with composer require some/bundle-name
then you can uninstall it the same way, replacing require
with remove
: composer remove some/bundle-name
. When you uninstall a package/bundle, the recipe will "uninstall" itself, which will remove any config, etc that was added when the bundle was installed.
Let me know if this helps!
Cheers!
Is there a way to get this working in Symfony 5 ?
In Terminal: composer require nexylan/slack-bundle:* php-http/guzzle6-adapter:*
I got the error message, when requiring the bundle:
Problem 1
- nexylan/slack-bundle v2.2.0 requires symfony/http-kernel ^3.4 || ^4.0 -> no matching package found.
- nexylan/slack-bundle v2.1.0 requires symfony/http-kernel ^3.4 || ^4.0 -> no matching package found.
- nexylan/slack-bundle v2.0.1 requires symfony/http-kernel ^3.4 || ^4.0 -> no matching package found.
- nexylan/slack-bundle v2.0.0 requires symfony/http-kernel ^3.4 || ^4.0 -> no matching package found.
- nexylan/slack-bundle v1.1.2 requires symfony/http-kernel ^2.7 || ^3.0 || ^4.0 -> no matching package found.
- nexylan/slack-bundle v1.1.1 requires symfony/http-kernel ^2.7 || ^3.0 -> no matching package found.
- nexylan/slack-bundle v1.1.0 requires symfony/http-kernel ^2.7 || ^3.0 -> no matching package foun.
- nexylan/slack-bundle v1.0.1 requires symfony/http-kernel ^2.7 || ^3.0 -> no matching package found
- nexylan/slack-bundle v1.0.0 requires symfony/http-kernel ^2.8 || ^3.0 -> no matching package found.
- Installation request for nexylan/slack-bundle * -> satisfiable by nexylan/slack-bundle[v1.0.0, v1.0.1, v1.1.0, v1.1.1, v1.1.2, v2.0.0, v2.0.1, v2.1.0, v2.2.0].
Installation failed, reverting ./composer.json to its original content.
When I run: $ composer require nexylan/slack-bundle php-http/guzzle6-adapter:1.1.1
I get . . .
[ErrorException]
curl_multi_setopt(): CURLPIPE_HTTP1 is no longer supported
Hey Daniel R.
I think you hit the same problem as mentioned in this thread https://github.com/symfony/...
The fix is to upgrade Symfony Flex (global installation as well in case you have it). Could you give it a try and let me know if it worked?
Cheers!
Hey Diego,
I think I'm trying the suggested solution, here's what happens:
APFVFXJ2F7HV2H:~ roushd$ composer global require symfony/flex ^1.5
Changed current directory to /Users/roushd/.composer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
Symfony recipes are disabled: "symfony/flex" not found in the root composer.json
Nothing to install or update
That makes me think you already have Flex installed but not for the latest version. Maybe if you runcompser update symfony/flex
?
Also, try requiring Flex by project basis and not globally
Cheers!
Hey Pedro,
Thank you for sharing this info with others! Though, it's weird, because in the screencast we see bundles were added automatically. Do you use symfony Flex in your project? What version of Symfony do you use? Anyway, adding it manually should work, thanks for this tip, might be useful for people who see the same error message.
Cheers!
I got it 'working' with
composer require nexylan/slack-bundle:1.1.2 php-http/guzzle6-adapter
'working' means getting the same error as in the tutorial
Unfortunately Http\HttplugBundle\HttplugBundle::class => ['all' => true], is not added to config/bundles.php and the file confgi/packages/httplug.yaml was not created either.
I do find however httplug in vendor/php-http/ and the composer require mentioned that it was installed
omposer require nexylan/slack-bundle:1.1.2 php-http/guzzle6-adapter
Using version ^2.0 for php-http/guzzle6-adapter
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Restricting packages listed in "symfony/symfony" to "4.1.*"
Prefetching 6 packages 🎵 💨
- Downloading (100%)
Package operations: 10 installs, 0 updates, 0 removals
- Installing guzzlehttp/promises (v1.3.1): Loading from cache
- Installing psr/http-message (1.0.1): Loading from cache
- Installing guzzlehttp/psr7 (1.4.2): Loading from cache
- Installing guzzlehttp/guzzle (6.3.3): Loading from cache
- Installing nexylan/slack (1.7.0): Loading from cache
- Installing nexylan/slack-bundle (v1.1.2): Loading from cache
- Installing psr/http-client (1.0.0): Loading from cache
- Installing php-http/promise (v1.0.0): Loading from cache
- Installing php-http/httplug (v2.0.0): Loading from cache
- Installing php-http/guzzle6-adapter (v2.0.0): Loading from cache
Writing lock file
Generating autoload files
Symfony operations: 1 recipe (1419766d96901a70366acb1ed9be6db3)
- Configuring nexylan/slack-bundle (>=v1.1.2): From auto-generated recipe
Executing script cache:clear [KO]
[KO]
Script cache:clear returned with error code 1
!!
!! In ArrayNode.php line 228:
!!
!! The child node "endpoint" at path "nexy_slack" must be configured.
!!
!!
!!
Script @auto-scripts was called via post-update-cmd
Hey Patrick!
It's awesome that you have found a working solution for this issue. We have checked packages and found that updated `php-http/guzzle6-adapter` caused it. Probably better solution will be to use
composer require nexylan/slack-bundle php-http/guzzle6-adapter:1.1.1
it will be closer to course code.
By the way we have already updated course page with this new command.
Cheers!.
using Symfony 4.1.7 I get the following error after
composer require nexylan/slack-bundle php-http/guzzle6-adapter
Using version ^2.0 for nexylan/slack-bundle
Using version ^2.0 for php-http/guzzle6-adapter
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Restricting packages listed in "symfony/symfony" to "4.1.*"
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Installation request for nexylan/slack-bundle ^2.0 -> satisfiable by nexylan/slack-bundle[v2.0.0].
- nexylan/slack v2.0.0 requires php-http/client-common ^1.7 -> satisfiable by php-http/client-common[1.7.0, 1.8.0, 1.8.1].
- php-http/client-common 1.7.0 requires php-http/httplug ^1.1 -> satisfiable by php-http/httplug[v1.1.0].
- php-http/client-common 1.8.0 requires php-http/httplug ^1.1 -> satisfiable by php-http/httplug[v1.1.0].
- php-http/client-common 1.8.1 requires php-http/httplug ^1.1 -> satisfiable by php-http/httplug[v1.1.0].
- Can only install one of: php-http/httplug[v2.0.0, v1.1.0].
- php-http/guzzle6-adapter v2.0.0 requires php-http/httplug ^2.0 -> satisfiable by php-http/httplug[v2.0.0].
- nexylan/slack-bundle v2.0.0 requires nexylan/slack ^2.0 -> satisfiable by nexylan/slack[v2.0.0].
- Installation request for php-http/guzzle6-adapter ^2.0 -> satisfiable by php-http/guzzle6-adapter[v2.0.0].
Installation failed, reverting ./composer.json to its original content.
and got it working by adding :
"nexylan/slack-bundle": "*",
"php-http/guzzle6-adapter":"*",
in the require part of the composer.json file
I got it 'working' with
composer require nexylan/slack-bundle:1.1.2 php-http/guzzle6-adapter
'working' means getting the same error as in the tutorial
Hmm, nexylan/slack-bundle
requires php-http/httplug v1.1
but php-http/guzzle6-adapter
requires php-http/httplug v2.0
so you can't have both versions installed, you need to find a release where both bundle versions can work together, you may want to contact the repository maintainer.
Cheers!
probably a different version of composer. or a newer release of php-http that is conflicting with the nexylan bundle. idk!
How to use multiple endpoints for Symfony NexySlackBundle
I am using the nexylan/slack Bundle for my symfony 3.4 application. I configured the slack Incoming WebHook for #general channel and it's working as expected. The bundle configuration looks something like:
nexy_slack:
# If you want to use an another httplug client service.
http:
client: httplug.client
# The Slack API Incoming WebHooks URL.
endpoint: https://hooks.slack.com/services/ABCD/987ABC
channel: null
username: null
icon: null
link_names: false
unfurl_links: false
unfurl_media: true
allow_markdown: true
markdown_in_attachments: []
Now I have another channel called #dev and I've added the Incoming WebHook and received the endpoint. I also want to send messages to the dev channel.
My question is, how can I configure the dev channel endpoint too in order to use it. Is there any way I can do this?
Hi Babagana Zannah!
Hmm. First, I may give a silly answer, because I'm trying to remember how this bundle works :). And, actually, the way the the incoming webhooks work is confusing. So, when you setup the webhook in Slack, you configure which channel you want to send to. But, when you actually *send* the message in PHP, there is also a to() method where you can set the channel: https://github.com/nexylan/...
So, I would try using this ONE endpoint and the to() method to try to send to the #dev channel. From my reading, it sounds like this may *not* work - but I'm not 100% sure... because it seems odd to have this to() method, if it never works. If it doesn't work and you DO need 2 endpoints then, yes, the bundle does not support this. It's not a HUGE deal, you will just need to configure a second "Nexy\Slack\Client" service manually on your own (i.e. in services.yml).
Cheers!
If you need second dev channel to work on other instance of your application (like prod slack channel - for prod environment, dev slack channel - for developers environment), than you need to specify endpoint as a parameter like
endpoint: '%slackEndpoint%'
and then specify this parameters in services.yaml and services_dev.yaml as it was done in the past tutorial of this course (https://knpuniversity.com/s....
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.0
"nexylan/slack-bundle": "^2.0,<2.2.0", // v2.0.0
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"sensio/framework-extra-bundle": "^5.1", // v5.1.4
"symfony/asset": "^4.0", // v4.0.4
"symfony/console": "^4.0", // v4.0.14
"symfony/flex": "^1.0", // v1.17.6
"symfony/framework-bundle": "^4.0", // v4.0.14
"symfony/lts": "^4@dev", // dev-master
"symfony/twig-bundle": "^4.0", // v4.0.4
"symfony/web-server-bundle": "^4.0", // v4.0.4
"symfony/yaml": "^4.0" // v4.0.14
},
"require-dev": {
"easycorp/easy-log-handler": "^1.0.2", // v1.0.4
"symfony/debug-bundle": "^3.3|^4.0", // v4.0.4
"symfony/dotenv": "^4.0", // v4.0.14
"symfony/maker-bundle": "^1.0", // v1.0.2
"symfony/monolog-bundle": "^3.0", // v3.1.2
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.0.4
"symfony/profiler-pack": "^1.0", // v1.0.3
"symfony/var-dumper": "^3.3|^4.0" // v4.0.4
}
}
I cannot get the nexylan slack bundle installed. My flex is up to date, I'm not on symfony 5.
This is what happens:
They're both added to bundles.php, but not to composer.json. Anytime I do anything in the terminal (cache:clear, debug:autowiring, whatever) it keeps telling me
In ArrayNode.php line 224: The child node "endpoint" at path "nexy_slack" must be configured.
Even though I created the nexy_slack.yaml with the required endpoint:
<b>Can I just continue with the course without nexylan/slack or do I need it in further chapters?</b>