I’m trying to find a good way to auto-include my modes/controller so i don’t need to repeat my self over and over to include them in the DI container. But is it safe, what do you recommend ?
example (when repeat my self)
// Model
$container['user'] = function ($container) {
return new \App\Models\User($container);
};
$container['products'] = function ($container) {
return new \App\Models\Product($container);
};
// controllers
$container['HomeController'] = function ($container) {
return new \App\Controllers\HomeController($container);
};
$container['AuthController'] = function ($container) {
return new \App\Controllers\AuthController($container);
};
and so on (doing this process by hand takes time and repeat)....
You can create your Model using the “new” keyword.
Try to keep it simple for the moment, then you can use some design patterns and make it more testable.
namespace App\Controllers;
use Slim\Views\Twig as View;
use \App\Models\Post as Post;
class PostController extends Controller{
public function index($request, $response){
Post::index();
return $this->view->render($response, 'posts.twig');
}
}
Post Model
namespace App\Models;
use Slim\Views\Twig as View;
class Post extends Model{
public static function index(){ // __invoke(){
echo 'asdasd';
}
}
Hi,
you could have a base controller and inject the dependencies you always (request, response, …) use in its constructor
For injecting models or other objects in the controller withoud having to type all that code in the routes I think the best solution is having some kind of automatic resolution like Laravel does.
Take a look at the link bellow. I have implemented a route resolver method that resolves and injects into the controller uri arguments, container dependencies or tries to automatically resolve and inject a object using reflection if the first two fail.
i have a tiny problem with my code now, i cant use $this->container bcs the Model.php is not getting called anywhere with the container in it, any ideas how to do it without injecting it to the container ?
Model.php
namespace App\Models;
use \Slim\PDO\Database as pdo;
class Model{
private $container;
public function __construct($container){
$this->container = $container;
}
public function __get($property){
if ($this->container->{$property}) {
return $this->container->{$property};
}
}
}
User.php
namespace App\Models;
use Slim\Views\Twig as View;
class User extends Model{
public function attempt($username, $password){
$stmt = $this->container->pdo->select()->from('account')->where('accountid', '=', $username);
$stmt = $stmt->execute();
$data = $stmt->fetch();
if ($data) {
// call pw fucntion for pw match
// ŕeturn $data;
}else{
return false;
}
}
}
$stmt = $pdo->select()->from('account')->where('accountid', '=', $username);
Type: Error
Message: Call to a member function select() on null
File: /var/www/hpseed.dev/app/Models/User.php
Line: 16