Here is the code for my index file which is in the root directory:
<?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();
Then, I have the Model.php
in a folder app\Model\Model.php
<?php
namespace app\Model;
use PDO;
class Model
{
function __construct($config)
{
}
}
I am getting this error:
Fatal error: Class 'app\Model\Model' not found in E:\xampp\htdocs\aaaaaslim\index.php on line 43
You can use Composer
to load your own namespace.
Just add following to your composer.json
.
"autoload": {
"psr-4": {
"app\\": "app/"
}
}
When you do composer update that autoload will know your namespace app
and include your classes on the fly without manually importing them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With