0
Structure of my project
In my Imports.php file I have the following code:
//UTIL
require_once 'util/Constants.php';
require_once 'util/Page.php';
//Control
require_once 'control/ViewController.php';
//View
require_once 'view/View.php';
In my start.php file I have the following code:
require_once 'Imports.php';
ini_set('display_errors', 1);
error_reporting(E_ALL);
ViewController::showView();
In my View.php class I have the following code:
class View {
private $control;
private $parametros;
private $user;
private $modulo = null;
function View() {
}
public function getControl() {
return $this -> control;
}
public function setControl($control) {
$this -> control = $control;
}
public function getParametros() {
return $this -> parametros;
}
public function setParametros($parametros) {
$this -> parametros = $parametros;
}
public function getUser() {
return $this->user;
}
public function setUser($user) {
$this->user = $user;
}
public function hasPermission(){
if($this->user!=null){
if($this->modulo!=null){
if(strcmp($this->modulo, $this->user->getTipo())!=0){
return false;
}
}
return true;
}
return false;
}
public function setModulo($modulo){
$this->modulo = $modulo;
}
public function getModulo(){
return $this->modulo;
}
}
In my class Viewcontroller.php I have the following code:
class ViewController {
private static $instance = null;
private static $root = null;
private static $currentUrl = null;
private static $view;
private function __construct() {}
public static function getInstance() {
//Contruir Url Atual
$server = $_SERVER['SERVER_NAME'];
$endereco = $_SERVER ['REQUEST_URI'];
self::$currentUrl = "http://" . $server . $endereco;
//Retornando a Instancia da página
if (!isset(self::$instance) && is_null(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
public static function redirect($pagina){
exit ('<script>top.location="'.$pagina.'"</script>');
}
public static function getCurrentUrl(){
self::$currentUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
return self::$currentUrl;
}
public static function getView(){
return self::$view;
}
public static function getAssetsSiteImg($image){
self::$root = ''.dirname( $_SERVER["PHP_SELF"] ).'';
if(self::$root=='/'){
self::$root = '';
}
echo self::$root.'/assets/site/img/'.$image;
}
public static function getAssetsSiteJs($js){
self::$root = ''.dirname( $_SERVER["PHP_SELF"] ).'';
if(self::$root=='/'){
self::$root = '';
}
echo self::$root.'/assets/site/js/'.$js;
}
public static function getAssetsLibJs($js){
self::$root = ''.dirname( $_SERVER["PHP_SELF"] ).'';
if(self::$root=='/'){
self::$root = '';
}
echo self::$root.'/assets/libs/js/'.$js;
}
public static function getAssetsLib($js){
self::$root = ''.dirname( $_SERVER["PHP_SELF"] ).'';
if(self::$root=='/'){
self::$root = '';
}
echo self::$root.'/assets/libs/'.$js;
}
public static function getRoot(){
self::$root = ''.dirname( $_SERVER["PHP_SELF"] ).'';
if(self::$root=='/'){
return '';
}else{
return self::$root;
}
}
public static function getRootPageAdmin($page){
self::$root = ''.dirname( $_SERVER["PHP_SELF"] ).'';
if(self::$root=='/'){
return "/coordenacao/".$page;
}else{
return self::$root."/coordenacao/".$page;
}
}
public static function getRootPage($page){
self::$root = ''.dirname( $_SERVER["PHP_SELF"] ).'';
if(self::$root=='/'){
echo "/coordenacao/".$page;
}else{
echo self::$root."/coordenacao/".$page;
}
}
public static function getRootPageSite($page){
self::$root = ''.dirname( $_SERVER["PHP_SELF"] ).'';
if(self::$root=='/'){
echo "/".$page;
}else{
echo self::$root."/".$page;
}
}
public static function redirectPage($pagina){
$pagina = ViewControl::getRoot()."/coordenacao/".$pagina;
exit ('<script>top.location="'.$pagina.'"</script>');
}
public static function redirectPageSite($pagina){
$pagina = ViewControl::getRoot()."/".$pagina;
exit ('<script>top.location="'.$pagina.'"</script>');
}
public static function redirectPageAdmin($pagina){
$pagina = ViewControl::getRoot()."/coordenacao/".$pagina;
exit ('<script>top.location="'.$pagina.'"</script>');
}
public static function getDominio(){
$dominio = $_SERVER['PHP_SELF'];
$dominio = explode("/", $dominio);
$dominio = array_slice($dominio, 1);
return $dominio[0];
}
public static function showView(){
$request = new FriendlyURL();
$view = new View();
$view -> setControl($request -> control);
$view -> setParametros($request -> params);
$view -> setUser(Sessao::getUsuario());
self::$view = $view;
if($view -> getParametros() == null){
$primeiroParametro = null;
}else{
$primeiroParametro = $request -> params[0];
}
if( ($view -> getControl() == Constants::ADMIN_DIR_NAME) && ($primeiroParametro == null) || ($view -> getControl() == Constants::ADMIN_DIR_NAME) && ($primeiroParametro == '') || ($view -> getControl() == Constants::ADMIN_DIR_NAME) && ($primeiroParametro == 'index') || ($view -> getControl() == Constants::ADMIN_DIR_NAME) && ($primeiroParametro == 'index.php') ){
$view -> setControl('index');
$old_array = $view -> getParametros();
$new_array = array();
$i = -1;
foreach ($old_array as $parametro) {
if($i != -1){
$new_array[$i]=$parametro;
}
$i++;
}
if($view -> getControl()=='index'){
$view -> setControl('home');
}
$view -> setParametros($new_array);
$filename = Constants::ADMIN_CAMINHO.$view -> getControl().'.php';
} else if( ($view -> getControl() == Constants::ADMIN_DIR_NAME) && ($view -> getParametros() != null) ){
$old_array = $view -> getParametros();
$new_array = array();
$i = -1;
foreach ($old_array as $parametro) {
if($i != -1){
$new_array[$i]=$parametro;
}
$i++;
}
if($old_array[0]=='msg'){
$view -> setControl('index');
}else{
$view -> setControl($old_array[0]);
}
if($view -> getControl()=='index'){
$view -> setControl('home');
}
$view -> setParametros($new_array);
$filename = Constants::ADMIN_CAMINHO.$view -> getControl().'.php';
} else {
$filename = Constants::SITE_CAMINHO.$view -> getControl().'.php';
}
if(file_exists($filename)){
include $filename;
}else{
//ViewControl::redirectPageSite(Page::PAGE_404);
}
}
}
Next mistake:
Fatal error: Class 'ViewController' not found in C:\xampp\htdocs\blogando\view\site\index.php on line 3
$thisView = ViewController::getView()
Note: I am using php 7, I already did a search and I did not find anything in relation to this.
I wrote following excerpt of code yet did not solve:
function __autoload($ViewController) {
if(file_exists($ViewController . '.php')) {
require_once ($ViewController . '.php');
}
else {
throw new Exception("Unable to load $ViewController");
}
}
try {
$class = new ViewController();
} catch (Exception $e) {
echo $e -> getMessage() , "\n";
}
No require, try using $_SERVER['DOCUMENT_ROOT']." /filename"; to see where it goes
– Sr. André Baill
Didn’t work out..
– Douglas William
Did you start her? Because there you mounted her just, did her boot?
– Sr. André Baill
$obj = new Viewcontroller();
– Sr. André Baill
Yes. $parameters = $thisView->getParametros();
– Douglas William
It is a framework or a custon MVC?
– Adriano Luz
custom MVC.....
– Douglas William
Have you considered using a
__autoload()
in its application?– Adriano Luz
I will not make a search. I have never used.
– Douglas William
edited my question with __autoload() did not resolve, my page does not appear.
– Douglas William