邮件发送/失败后重定向并显示Slim Flash消息

时间:2018-12-09 21:43:05

标签: php slim

我是Slim的新手。一切都很好,但是我尝试引入Slim-Flash软件包,并希望在联系表单消息发送或失败时利用通知用户的方法。

我使用PhpMailer的标准方法。为了保持路线整洁,我创建了一个类来处理所有正常工作的邮件:

class Mailer
{
   public function __invoke($request, $response, $args) use ($app) {
   // SMTP & Fields
   }
}

成功后,我想执行重定向并显示类似这样的苗条消息:

$this->flash->addMessage('global', 'Message sent!');
return $response->withRedirect($app->router->pathFor('contact'));

但是,当我尝试执行操作时,遇到以下错误:

Call to a member function getContainer() on null

我还很新,在这里我显然缺少一些东西-任何提示都将非常有帮助。

这是完整的跟踪记录:

#0 /Applications/MAMP/htdocs/portalSlim/vendor/composer/ClassLoader.php(444): include()
#1 /Applications/MAMP/htdocs/portalSlim/vendor/composer/ClassLoader.php(322): Composer\Autoload\includeFile('/Applications/M...')
#2 [internal function]: Composer\Autoload\ClassLoader->loadClass('App\\Mail\\Mailer')
#3 [internal function]: spl_autoload_call('App\\Mail\\Mailer')
#4 /Applications/MAMP/htdocs/portalSlim/vendor/slim/slim/Slim/CallableResolver.php(89): class_exists('App\\Mail\\Mailer')
#5 /Applications/MAMP/htdocs/portalSlim/vendor/slim/slim/Slim/CallableResolver.php(67): Slim\CallableResolver->resolveCallable('App\\Mail\\Mailer')
#6 /Applications/MAMP/htdocs/portalSlim/vendor/slim/slim/Slim/CallableResolverAwareTrait.php(45): Slim\CallableResolver->resolve('App\\Mail\\Mailer')
#7 /Applications/MAMP/htdocs/portalSlim/vendor/slim/slim/Slim/Route.php(351): Slim\Routable->resolveCallable('App\\Mail\\Mailer')
#8 /Applications/MAMP/htdocs/portalSlim/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(117): Slim\Route->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#9 /Applications/MAMP/htdocs/portalSlim/vendor/slim/slim/Slim/Route.php(334): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#10 /Applications/MAMP/htdocs/portalSlim/vendor/slim/slim/Slim/App.php(515): Slim\Route->run(Object(Slim\Http\Request), Object(Slim\Http\Response))
#11 /Applications/MAMP/htdocs/portalSlim/vendor/slim/csrf/src/Guard.php(171): Slim\App->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#12 [internal function]: Slim\Csrf\Guard->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
#13 /Applications/MAMP/htdocs/portalSlim/vendor/slim/slim/Slim/DeferredCallable.php(43): call_user_func_array(Object(Slim\Csrf\Guard), Array)
#14 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
#15 /Applications/MAMP/htdocs/portalSlim/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
#16 /Applications/MAMP/htdocs/portalSlim/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(117): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#17 /Applications/MAMP/htdocs/portalSlim/vendor/slim/slim/Slim/App.php(406): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#18 /Applications/MAMP/htdocs/portalSlim/vendor/slim/slim/Slim/App.php(314): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#19 /Applications/MAMP/htdocs/portalSlim/public/index.php(7): Slim\App->run()
#20 {main}

1 个答案:

答案 0 :(得分:1)

使用use关键字来使__invoke方法(属于Mailer类的成员,而不是anonymous function的方法)可以像访问$app一样

public function __invoke($request, $response, $args) use ($app)

不会执行您期望的操作,并且会引发语法错误。 Slim在需要创建类的新实例时将容器实例传递给类构造函数,因此您的Mailer类应具有一个__construct方法,该方法接受一个Psr\Container\ContainerInterface参数。然后,您可以从容器($this->continer->router而不是$app->router)中检索路由器:

use Psr\Container\ContainerInterface

class Mailer
{
   protected $container;

   public function __construct(ContainerInterface $container) {
       $this->container = $container;
   }
   public function __invoke($request, $response, $args) {
       $this->container->flash->addMessage('global', 'Message sent!');
       return $response->withRedirect($this->container->router->pathFor('contact'));
   }
}