我正在制作一个简单的应用程序并学习Slim 3框架。 当我想要MVC设计并与DIC一起工作时,我尝试了一个简单的教程并将其工作放在上面。
从理论上讲这很简单。
问题,我不知道如何获取我对模型的依赖关系才能调用PDO,但出现此错误。
无需在模型上传递DIC,设计和应用程序就可以正常工作。 我可以从控制器中调用模型,并在视图上传递任何内容,而不会出现任何错误。
下面是我的代码,在此先感谢您的启发。
从路线开始,我得到了:
$app->get('/content', \App\Controllers\ContentController::class);
控制器
<?php
namespace App\Controllers;
class ContentController
{
protected $container;
// Passes the DIC to get the model.
function __construct($container)
{
$this->container = $container;
}
function __invoke($request, $response, $args)
{
$datas = $this->container->get('contentModel');
$args['content'] = $datas->testContent();
// get the template renderer and pass response and datas to the template file.
return $this->container->get('renderer')->render($response, 'content.php', $args);
}
这是我的模型:
<?php
namespace App\Models;
class Model
{
// Passes the DIC to get db later.
function __construct($container)
{
$this->container = $container;
}
protected function executeQuery($sql, $params = null) {
if ($params == null)
{
$result = $this->container->get('db')->query($sql); // direct execution
} else {
$result = $this->container->get('db')->prepare($sql); // prepared execution
$result->execute($params);
}
return $result;
}
}
我的内容模型
<?php
namespace App\Models;
Class ContentModel extends Model
{
public function testContent()
{
$testDatas = "Hello world";
return $testDatas;
}
public function getContent()
{
$sql = 'SELECT * FROM posts';
$posts = $this->executeQuery($sql);
return $posts;
}
}
这是我的依赖项:
<?php
use Slim\App;
return function (App $app) {
$container = $app->getContainer();
// View renderer
$container['renderer'] = function ($c) {
$settings = $c->get('settings')['renderer'];
return new \Slim\Views\PhpRenderer($settings['template_path']);
};
// Database
$container['db'] = function ($c) {
$db = $c['settings']['db'];
$pdo = new PDO('mysql:host=' . $db['host'] . ';dbname=' . $db['dbname'],
$db['user'], $db['pass']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $pdo;
};
// Model data stored
$container['contentModel'] = new \App\Models\ContentModel();
};
每当我想在模型中传递DIC时,都会出现此错误,但是构造函数在我的控制器上工作得很好。
(!第8行的C:\ wamp \ www \ slimappmvc \ src \ Classes \ Models \ Model.php中应该有1个
(!)ArgumentCountError:函数App \ Models \ Model :: __ construct()的参数太少,第35行的C:\ wamp \ www \ slimappmvc \ src \ dependencies.php中传递了0,而在C语言中恰好传递了1 :第8行上的\ wamp \ www \ slimappmvc \ src \ Classes \ Models \ Model.php
答案 0 :(得分:0)
ContentModel使用与Model类相同的构造函数,因为扩展时并未覆盖它,并且在实例化类“ $ container”时,Model的构造函数需要1个参数。因此,当您创建实例ContentModel时,还需要向其传递一个参数:
<?php
mysql_connect("xxx","root","xxx");
mysql_select_db("alter");
if(isset($_POST["submit1"]))
{
$image = addslashes(file_get_contents($_FILES['f1']['tmp_name']));
mysql_query("insert into users values('','$image')");
}
if(isset($_POST["submit2"]))
{
$res=mysql_query("select * from users");
echo "<table>";
echo "<tr>";
while($row=mysql_fetch_array($res))
{
echo "<td>";
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image1'] ).'" height="200" width="200"/>';
echo "<br>";
?><a href="delete.php?id=<?php echo $row["id"]; ?>">Delete</a> <?php
echo "</td>";
}
echo "</tr>";
echo "</table>";
}
?>
</body>
</div>
<script>
// Get the Sidebar
var mySidebar = document.getElementById("mySidebar");
// Get the DIV with overlay effect
var overlayBg = document.getElementById("myOverlay");
// Toggle between showing and hiding the sidebar, and add overlay effect
function w3_open() {
if (mySidebar.style.display === 'block') {
mySidebar.style.display = 'none';
overlayBg.style.display = "none";
} else {
mySidebar.style.display = 'block';
overlayBg.style.display = "block";
}
}
// Close the sidebar with the close button
function w3_close() {
mySidebar.style.display = "none";
overlayBg.style.display = "none";
}
</script>
我假设$ container变量是Model类所期望的。