dtyz76562 2019-04-03 09:05
浏览 48

如何从twig文件中调用服务以在URL中显示加密参数?

I have a anchor tag:

a href={{ path('edit_teacher', { slug: teacher.id}) }}>Edit</a

which gives URL like:

localhost:9000/edit-teacher/1

Here, I want to encrypt parameter '1' as I don't want the user to see the teacher Id. So I made a service to encrypt this Id.

Service :

namespace App\Service;

class EncryptGenerator
{
    /**
     * This function incrypts the data provided.
     * @param: int/string.
     */
    public function incryptData($data)
    {
        $incryptedData = convert_uuencode($data);
        return $incryptedData;
    }

    /**
     * This function decrypts the strings provided.
     * @param: string. 
     */
    public function decryptedData($string)
    {
        $decryptedData = convert_uudecode($string);
        return $decryptedData;
    }
}

I have also added this service to twig file in twig.yaml.

twig:
    default_path: '%kernel.project_dir%/templates'
    debug: '%kernel.debug%'
    strict_variables: '%kernel.debug%'
    form_themes: ['bootstrap_4_layout.html.twig']
    globals:
        my_service: "@App\\Service\\EncryptGenerator"

Lastly, I am not able to call this service by my twig file, below is what I am trying to anchor tag:

a href={{ path('edit_teacher',{ my_service.incryptData( slug: teacher.id }) }) }}>Edit

which gives me following error:

A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{".**

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 gdf格式的脑电数据如何处理matlab
    • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
    • ¥100 监控抖音用户作品更新可以微信公众号提醒
    • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
    • ¥70 2048小游戏毕设项目
    • ¥20 mysql架构,按照姓名分表
    • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
    • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
    • ¥15 linux驱动,linux应用,多线程