symfony登录和登录错误提示修改

PHP cooljun 228℃ 0评论

修改config.yml配置  增加translator:en

framework:
    esi: { enabled: true }
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    templating:
        engines: ['twig']
    default_locale:  "%locale%"
    trusted_hosts:   ~
    trusted_proxies: ~
    session:
        handler_id:  session.handler.native_file
        save_path:   "%kernel.root_dir%/../var/sessions/%kernel.environment%"
    fragments:       ~
    http_method_override: true
    assets: ~
    translator: { fallback: en }/***加的就是这句****

AdminBundle/translations/messages.en.yml   根据对应路径创建messages文件

messages.en.yml 中en 要和config.yml中添加的配置对应,下面是登录对应的一些错误信息

Bad credentials.  : "密码错误!"
Invalid CSRF token. : "token过期,请重新刷新页面!"

SecurityController对应代码

namespace AdminBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use AdminBundle\Controller\CommonController;
use Symfony\Component\Security\Core\Security;

class SecurityController extends CommonController
{
    /**
     * @Route("/login", name="login_route")
     */
    public function loginAction(Request $request)
    {
        $curUser = $this->getUser();
        if ($curUser) {
            return $this->redirectToRoute("admin_admin_index");
        }
        $session = $request->getSession();
        $error = '';
        if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
            $error = $request->attributes->get(Security::AUTHENTICATION_ERROR);
        } elseif (null !== $session && $session->has(Security::AUTHENTICATION_ERROR)) {
            $error = $session->get(Security::AUTHENTICATION_ERROR);
            $session->remove(Security::AUTHENTICATION_ERROR);
        } else {
            $error = '';
        }

        $lastUsername = (null === $session) ? '' : $session->get(Security::LAST_USERNAME);
        return $this->show('admin/login',['last_username' => $lastUsername,'error'=> $error]);
    }

    /**
     * @Route("/dologin",name="login_check")
     */
    public function checkAction(Request $request)
    {
    }

    /**
     * @Route("/logout",name="logout")
     */
    public function logoutAction(Request $request)
    {
    }
}

登录模版twig代码

symfony登录和登录错误提示修改

登录效果图

未修改前提示Bad credentials.,该功能类似TP Language 多语言支持的功能

symfony登录和登录错误提示修改

转载请注明:cooljun小站 » symfony登录和登录错误提示修改

如果你觉得这篇文章对你有帮助,请支持我继续更新网站 !捐赠本站
喜欢 (0)or分享 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址