0
I’m using routing with Drouter and always need to take the data of such store by Slug, for this I need to do 1 select
on all routes.
But I don’t know how to get the id, name and Slug from over Sistemamodel.php and so on index php. using the variable $dados
.
Says that the variable $dados
that does not exist and that is not an object.
index php.
$app = new DRouter\App();
$app->render->setViewsFolder(__DIR__.'/App/Views/');
require 'App/Controllers/SistemaController.php';
/* Login */
$app->get('/:slug/admin', function($slug){
SistemaController::dadosLoja($slug);
$this->render->load('lojista/loj_login.php', [
'id_loja' => $dados->id,
'nome_loja' => $dados->nome,
'slug_loja' => $dados->slug
]);
});
Sistemacontroller.php
require dirname(__FILE__) . '/../Models/SistemaModel.php';
class SistemaController
{
public static function dadosLoja($slug){
$dados = SistemaModel::slug($slug);
if($dados != false){
return $dados;
}
}
}
Sistemamodel.php
class SistemaModel
{
public static function slug($slug){
$dados = DB::conn()->prepare("SELECT * FROM `admin_lojas` WHERE `slug` = :slug");
$dados->bindValue(':slug', $slug, PDO::PARAM_STR);
$dados->execute();
if(count($dados) == 1){
return $dados;
}
}
}
DB.php
class DB
{
private static $conn;
public function __construct(){}
public static function conn(){
try {
if(is_null(self::$conn)){
self::$conn = new PDO('mysql:host='.HOST.';dbname='.BD.'',''.USER.'',''.PASS.'',
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
));
}
return (self::$conn) ? self::$conn : false;
} catch(PDOException $e) {
die("Error");
}
}
}
Are you using Poser-autoloader? You really need to use
require
? I mean if you use Poser-autoloader wouldn’t it be easier to set the namespaces in Composer.json? Set the classDB::conn
, there is no way to know if it is a framework or if it was you who created it.– Guilherme Nascimento
@I’m using the Drouter for routing
– Jonathan Silva