如何从Silex中的url获取参数

时间:2016-04-04 13:29:31

标签: php symfony silex

我试图从Silex应用中的网址获取参数。

这是我的控制器的连接方法:

public function connect(Application $app)
{
    $controllers = $app['controllers_factory'];

    $controllers->get('/list/pois/{id}', array($this, 'actionPoisList'))
        ->before(array($this, 'controlerAuthentification'));

    return $controllers;
}

我试图通过这样做来捕捉这个参数:

/**
 * @param Application $app
 * @return \Symfony\Component\HttpFoundation\JsonResponse
 */
public function actionPoisList(Application $app){

    return $app->json($app->escape($app['id']));
}

显然,它没有工作所以任何替代方案请。感谢

3 个答案:

答案 0 :(得分:1)

如果您在参数列表中指定URL中的参数,则会自动将其传递到控制器路径中:

/**
 * @param Application $app
 * @return \Symfony\Component\HttpFoundation\JsonResponse
 */
public function actionPoisList(Application $app, $id){

    return $app->json($app->escape($id));
}

考虑到route参数和函数参数的名称应完全相同。

答案 1 :(得分:0)

这通常被称为url slug,而silex文档正在将其考虑在内here

基本上你只是将你的路线解析的函数中的变量传递给

public static PDDocument replaceText(PDDocument document, String searchString, String replacement) throws IOException {
    if (Strings.isEmpty(searchString) || Strings.isEmpty(replacement)) {
        return document;
    }
    PDPageTree pages = document.getDocumentCatalog().getPages();
    for (PDPage page : pages) {
        PDFStreamParser parser = new PDFStreamParser(page);
        parser.parse();
        List tokens = parser.getTokens();
        for (int j = 0; j < tokens.size(); j++) {
            Object next = tokens.get(j);
            if (next instanceof Operator) {
                Operator op = (Operator) next;
                //Tj and TJ are the two operators that display strings in a PDF
                if (op.getName().equals("Tj")) {
                    // Tj takes one operator and that is the string to display so lets update that operator
                    COSString previous = (COSString) tokens.get(j - 1);
                    String string = previous.getString();
                    string = string.replaceFirst(searchString, replacement);
                    previous.setValue(string.getBytes());
                } else if (op.getName().equals("TJ")) {
                    COSArray previous = (COSArray) tokens.get(j - 1);
                    for (int k = 0; k < previous.size(); k++) {
                        Object arrElement = previous.getObject(k);
                        if (arrElement instanceof COSString) {
                            COSString cosString = (COSString) arrElement;
                            String string = cosString.getString();
                            string = StringUtils.replaceOnce(string, searchString, replacement);
                            cosString.setValue(string.getBytes());
                        }
                    }
                }
            }
        }
        // now that the tokens are updated we will replace the page content stream.
        PDStream updatedStream = new PDStream(document);
        OutputStream out = updatedStream.createOutputStream();
        ContentStreamWriter tokenWriter = new ContentStreamWriter(out);
        tokenWriter.writeTokens(tokens);
        page.setContents(updatedStream);
        out.close();
    }
    return document;
}

答案 2 :(得分:0)

对于不熟悉Silex框架的人们,我认为在控制器动作的签名中包含参数会模糊其起源。

我个人更喜欢不将其包含在方法的签名中,而是通过请求对象进行检索,这凸显了在路由中包含参数的事实。这可以通过for (let i = 0; i < 20; i++) { //do something with your response... let response = element(by.css(helperSessionItemSearch(`div.scroll-content ion-grid:nth-child(5) session-item:nth-child(${loopRun})`))) //do your stuff } 属性来完成:

attribute