【问题标题】:Redirecting to route with text AND variables?使用文本和变量重定向到路由?
【发布时间】:2014-12-22 05:39:00
【问题描述】:

我目前正在使用 Laravel 4 和 Sentry 2 来开发一个非常简单的启用限制的身份验证系统。也就是说,当用户尝试登录时引发异常时,因为他已被暂停,他应该被重定向回登录视图,并显示一条消息“此用户已暂停 $time 分钟。”

问题是:我无法将变量和文本消息都返回到视图。也许我错过了正确的语法,我不知道。这一直让我发疯,因为我需要为许多其他例外情况这样做。这似乎是一个新手问题,解决方案应该很简单。

这是我代码中重要的部分:

catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e)
{
    $throttle = Sentry::findThrottlerByUserLogin(Input::get('username'));
    $time = $throttle->getSuspensionTime();

     return Redirect::route('account-sign-in-get')
                    ->with('message','This user has been suspended for [$time] minutes.');


}

提前致谢。

【问题讨论】:

  • 在php手册中查找变量插值或字符串连接

标签: php laravel exception cartalyst-sentry


【解决方案1】:

只需替换

->with('message','This user has been suspended for [$time] minutes.');

->with('message', "This user has been suspended for {$time} minutes.");

实际情况:

在 PHP 中,当您在字符串中使用单引号时,它不会解析嵌入在该字符串中的变量,但是当您使用双引号时,它会解析它。

【讨论】:

  • 我的错!我似乎总是忘记单引号和双引号在 PHP 中的工作方式不同。工作就像一个魅力,非常感谢你!
猜你喜欢
  • 2013-03-29
  • 2017-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-09
  • 2017-06-01
  • 2020-06-28
相关资源
最近更新 更多