我正在使用Silex PHP框架。
我有两个文件,第一个是app.php:
<?php
require_once __DIR__.'/../vendor/autoload.php';
use Symfony'Component'HttpFoundation'Request;
use Symfony'Component'HttpFoundation'Response;
use Symfony'Component'HttpFoundation'ParameterBag;
use Silex'ControllerProviderInterface;
$app = new Silex'Application();
$app['debug'] = true;
$app->register(new Silex'Provider'DoctrineServiceProvider(), array(
'db.options' => array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'dbname' => 'testapp',
'user' => 'root',
'password' => 'turoke55u',
),
));
$app->before(function (Request $request) {
if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
$data = json_decode($request->getContent(), true);
$request->request->replace(is_array($data) ? $data : array());
}
});
$app->mount('/', include 'login.php');
$app->run();
第二个是登录.php:
<?php
use Symfony'Component'HttpFoundation'Request;
$login = $app['controllers_factory'];
$login->post('/apiv1/user/login', function (Request $request) use ($login) {
$userinfo = array(
'email' => $request->request->get('email'),
'mode' => $request->request->get('mode'),
'password' => $request->request->get('password'),
);
$passwordcoding = sha1($userinfo['email']."66643asd");
$emailverification = "SELECT email,password FROM user WHERE email='".$userinfo['email']."'";
$selectemail = $login['db']->$fetchAll('SELECT * FROM user');
var_dump($emailverification);
});
return $login;
当我在数据库上运行选择时,出现此错误:
不能将 Silex''ControllerCollection 类型的对象用作第 13 行/mnt/hgfs/Share_Folder/frontend/src/login.php 中的数组
有人知道该问题的解决方案吗?
我有第二个问题,为什么如果我在应用程序中更改.php这个:
$app->mount('/', include 'login.php');
对此:
$app->mount('/apiv1/user/login', include 'login.php');
登录后.php:
$login->post('/',
就像在 Silex 文档中一样,框架不起作用?
你的函数签名中有一个愚蠢的错误:
<?php
use Symfony'Component'HttpFoundation'Request;
$login = $app['controllers_factory'];
$login->post('/apiv1/user/login', function (Request $request) use ($login) {
// ^^^^^^^^
// this should be $app, not $login as this function is using the container
$userinfo = array(
'email' => $request->request->get('email'),
'mode' => $request->request->get('mode'),
'password' => $request->request->get('password'),
);
$passwordcoding = sha1($userinfo['email']."66643asd");
$emailverification = "SELECT email,password FROM user WHERE email='".$userinfo['email']."'";
// here you're using the $login as the container and it's a controller collection, so in the signature use the container not the $login
// vvvvvvvvvvvv
$selectemail = $login['db']->$fetchAll('SELECT * FROM user');
var_dump($emailverification);
});
return $login;
至于第二个疑问,您应该详细说明"框架不起作用",解释症状和输出(如果错误哪个错误?