I'm new to Silex and I'm really messed up with it.
I'm trying to do a simple login form and I don't know how to properly handle the POST submissions of my form.
我是Silex的新手,我真的搞砸了。我正在尝试做一个简单的登录表单,我不知道如何正确处理我的表单的POST提交。
I know I have to set something up in my Silex index, but I don't know how to do it. Basically, what I want is to be able to do something with the post values of the form. This is my actual code:
我知道我必须在我的Silex索引中设置一些内容,但我不知道该怎么做。基本上,我想要的是能够使用表单的post值做一些事情。这是我的实际代码:
<?php
require_once __DIR__.'/protected/vendor/autoload.php';
use Herrera\Template\TemplateServiceProvider;
use Silex\Application;
use Symfony\Component\Yaml\Parser;
use Phinx\Db\Adapter;
$parser = new Parser();
$config = $parser->parse(file_get_contents(__DIR__ .DIRECTORY_SEPARATOR.'protected'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR . 'main.yml'));
$app = new Application();
$app->register(new TemplateServiceProvider(), array('template.dir' => __DIR__ . '/protected/views/'));
/** @var Herrera\Template\Engine $engine */
$engine = $app['template.engine'];
$app->get('/admin', function() use($engine, $config) {
return $engine->render('admin.php', array(
'static_url' => $config['static_url'],
'title' => 'Admin'
), true);
});
$app->run();
?>
And well, my admin.php file, where the form is, is a simple html form.
I've been searching for answers for this, but I haven't found anything I could understand/do...
好吧,表单所在的admin.php文件是一个简单的html表单。我一直在寻找这方面的答案,但我找不到任何我能理解/做的事......
Hope you can help me!
Thanks!
希望你能帮我!谢谢!
1 个解决方案
#1
You should create a POST
route that will be used after your form is submitted. Then access form values in Request
object. For example:
您应该创建一个将在提交表单后使用的POST路由。然后访问Request对象中的表单值。例如:
$app->post('/admin', function (Request $request) {
$login = $request->get('login');
$password = $request->get('password');
// Further processing
});
Have look at docs: http://silex.sensiolabs.org/doc/usage.html#example-post-route
看看文档:http://silex.sensiolabs.org/doc/usage.html#example-post-route
#1
You should create a POST
route that will be used after your form is submitted. Then access form values in Request
object. For example:
您应该创建一个将在提交表单后使用的POST路由。然后访问Request对象中的表单值。例如:
$app->post('/admin', function (Request $request) {
$login = $request->get('login');
$password = $request->get('password');
// Further processing
});
Have look at docs: http://silex.sensiolabs.org/doc/usage.html#example-post-route
看看文档:http://silex.sensiolabs.org/doc/usage.html#example-post-route