2016-09-10 83 views
0

制作我的第一个Silex应用程序,我需要一些帮助,我正在写一条路线来显示一个项目然后进行编辑,因此首先这是我的'get'路线,它接收项目的代码以显示其细节。如何在Silex 2中发送可变的路由到路由?

$app->get('/cat/productos_edit/{key}', function($key) use($app){ 

$app['twig']->addGlobal('itemtoedit', $key); //This is how I'm trying to do it 

return $app['twig']->render('catalogo/productos/edit.html',[ 
    'title' => 'Catálogo - Productos' 
    ]); 
}); 

所以在 '后' 的路线,我需要得到该变量编辑的项目,

$app->post('/cat/productos_edit/', function() use($app){ 
    echo $app['itemtoedit']; 
})->bind('cat.productos.edit'); 

但后来我得到这个错误:

Silex error

所以我觉得也许我错过了什么,希望ypu能帮助我。

回答

3

PHP/Silex的不会让你之间获得任何上下文,并张贴所以恕我直言,你应该做的:

$app->post('/cat/productos_edit/{key}', function($key) use($app){ 
    // get your item from database with its key 
    // update item with your post payload 
})->bind('cat.productos.edit'); 
+0

Definetely的“绝招”是使用你用于获取发布网址相同的密钥发布新数据的项目。请务必进行安全检查,确保任何人都可以将数据发布到特定网址并更改您的项目! (这是CSRF令牌为什么创建的原因) – mTorres

+0

谢谢,是的,我想我会做一个隐藏的输入来保存该密钥,当然我已经实现了CSRF保护 – DaveSanchez

+0

或者只是将您的提交网址链接到' cat.productos.edit'在你的树枝模板中 – seblucas