【问题标题】:Slim class not found.I am not sure what is it am doing wrong here未找到 Slim 类。我不确定它在这里做错了什么
【发布时间】:2015-08-19 17:26:44
【问题描述】:

这是我在根目录中的索引文件的代码:

<?php
require 'vendor/autoload.php';
date_default_timezone_set('Asia/Seoul');

//$log = new Monolog\Logger('name');
//$log->pushHandler(new Monolog\Handler\StreamHandler('app.txt', Monolog\Logger::WARNING));
//$log->addWarning('Oh Noes.');

$app = new \Slim\Slim( array(
  'view' => new \Slim\Views\Twig()
));
$app->add(new \Slim\Middleware\SessionCookie());

$view = $app->view();
$view->parserOptions = array(
    'debug' => true
);
$view->parserExtensions = array(
    new \Slim\Views\TwigExtension(),
);
//Path To Our View
$app->view->setTemplatesDirectory("app/view");

// Configs for mode "production"
$app->configureMode('production', function () use ($app) {
    // Set the configs for production environment
    $app->config(array(
        'debug' => false,
        'database' => array(
            'db_host' => 'localhost',
            'db_port' => '',
            'db_name' => 'mini',
            'db_user' => 'root',
            'db_pass' => 'yaounde'
        )
    ));
});


/******************************************** THE MODEL ********************************************************/

// Initialize the model, pass the database configs. $model can now perform all methods from Mini\model\model.php
$model = new \app\Model\Model($app->config('database'));

/************************************ THE ROUTES / CONTROLLERS *************************************************/

$app->get('/', function() use($app){
  $app->render('about.twig');
})->name('home');

$app->get('/contact', function() use($app){
  $app->render('contact.twig');
})->name('contact');

$app->post('/contact', function() use($app){
  $name = $app->request->post('name');
  $email = $app->request->post('email');
  $msg = $app->request->post('msg');

  if(!empty($name) && !empty($email) && !empty($msg)) {
    $cleanName = filter_var($name, FILTER_SANITIZE_STRING);
    $cleanEmail = filter_var($email, FILTER_SANITIZE_EMAIL);
    $cleanMsg = filter_var($msg, FILTER_SANITIZE_STRING);
  } else {
    $app->flash('fail', 'All Fields Are Required.');
    $app->redirect('contact');
  }

  $transport = Swift_MailTransport::newInstance();
  $mailer = \Swift_Mailer::newInstance($transport);

  $message = \Swift_Message::newInstance();
  $message->setSubject('Simple Blog Contact Form Submission');
  $message->setFrom(array(
     $cleanEmail => $cleanName
  ));
  $message->setTo(array('admin@localhost'));
  $message->setBody($cleanMsg);

  $result = $mailer->send($message);

  if($result > 0) {
    $app->flash('success', 'Thanks So Much! You are AWESOME!!!');
    $app->redirect('contact');

  } else {
    $app->flash('fail', 'So Sorry, Something Went Wrong. Please Try Again!');
    // log that there was an error
    $app->redirect('/contact');
  }


});

$app->run();

然后,我在文件夹 app\Model\Model.php 中有 Model.php

<?php

namespace app\Model;

use PDO;

class Model
{

    function __construct($config)
    {

    }
}

我收到此错误:

致命错误:在第 43 行的 E:\xampp\htdocs\aaaaaslim\index.php 中找不到类 'app\Model\Model'

【问题讨论】:

  • 您是否包含Model.php
  • 工作,当我做包含时,我认为我不需要做任何其他包含,因为我有 autoload.php

标签: php slim


【解决方案1】:

您可以使用Composer 来加载您自己的命名空间。

只需将关注添加到您的composer.json

"autoload": {
    "psr-4": {
        "app\\": "app/"
    }
}

当您执行 composer update 时,自动加载将知道您的命名空间 app 并即时包含您的类,而无需手动导入它们。

【讨论】:

  • 我曾经在 psr-4 中为 app 文件夹中的每个文件夹设置一行。(Controller、Model、Helper、...)您所写的仅使用一行解决了问题。
猜你喜欢
  • 2017-09-29
  • 2019-12-07
  • 2020-12-23
  • 1970-01-01
  • 1970-01-01
  • 2016-03-03
  • 2013-06-28
  • 2023-01-19
  • 1970-01-01
相关资源
最近更新 更多