精简的程序结构
我想向我的 Slim 应用程序添加一个函数,但我对 PHP 不够熟悉,不知道构建此类内容的最佳方法。这不是生产就绪的代码,我显然不会将我的用户名和密码硬编码到脚本中。我这样做只是为了说明这个概念。
$options = array(
'username' => 'admin',
'password' => 'password'
);
$app = new Slim(array(
'view' => new TwigView()
));
$app->config($ptions);
function authenticate($app, $username, $password) {
if($username==$app->config('username') && $password==$app->config('password')){
return true;
} else {
return false;
}
}
$app->get('/', function () use ($app) { // ... }
// ... other routes
$app->post('/login', function() use ($app) {
$username = $app->request()->post('username');
$password = $app->request()->post('password');
if(authenticate($app, $username,$password)) {
$app->redirect('/');
}
$app->redirect('/login');
});
$app->run();
必须将 $app
传递给 authenticate()
是否有意义,或者是否有更好的方法? authenticate()
不是中间件,而是在 POST 路由中调用的函数,用于在登录表单上按提交。
I wanted to add a function to my Slim application but I'm not familiar enough with PHP to know the best way to structure something like this. This is not production ready code and I obviously will not be hard coding my username and password into the script. I made this simply to illustrate the concept.
$options = array(
'username' => 'admin',
'password' => 'password'
);
$app = new Slim(array(
'view' => new TwigView()
));
$app->config($ptions);
function authenticate($app, $username, $password) {
if($username==$app->config('username') && $password==$app->config('password')){
return true;
} else {
return false;
}
}
$app->get('/', function () use ($app) { // ... }
// ... other routes
$app->post('/login', function() use ($app) {
$username = $app->request()->post('username');
$password = $app->request()->post('password');
if(authenticate($app, $username,$password)) {
$app->redirect('/');
}
$app->redirect('/login');
});
$app->run();
Does it make sense to have to pass $app
to authenticate()
or is there a better way? authenticate()
would not be middleware, but a function called in the POST route for pressing submit on a login form.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议你使用注册表方法.. ohbtw
$app->config($ptions);
应该是$app->config($options);
至于“注册表”,我使用以下类:
保存使用
检索使用
I suggest you use registry method.. ohbtw
$app->config($ptions);
should be$app->config($options);
as for "registry", I use the following class:
to save use
to retrieve use