autoload in classes using mvc

Asked

Viewed 545 times

-2

You can do this in php

use COntroller;
class Bootstrapp(){

public static function logar(){
$controller=new \Controller\$controlador();
}

I’ve tried it here and the syntax error

down here the code

if (is_readable($caminho)) {

            require $caminho;
            $controlador = ucfirst($controller);
           ( $controller = new \controllers\$controlador;) aqui onde da erro

            if (is_callable(array($controller, $metodo))) {

                $metodo = $pedido->getMetodo();
            } else {
                $metodo = "index";
            }

            if (isset($parametro)) {
                call_user_func_array(array($controller, $metodo), $parametro);
            } else {
                call_user_func(array($controller, $metodo));
            }
        } else {
            header("Location:" . URL . "error");
        }
  • And what are you trying to do to yourself? You can put the code?

  • 1

    There’s something wrong with this "MVC", log in should not be a bootstrap method! Login is a user request and needs logic (model), so it should be a controller.

  • MVC has nothing to do with autoload php.. I think what you want is to autoload classes with namespace. First try to understand the standards for autoload, see this: http://www.php-fig.org/psr/psr-0/

  • I read the question five times and I don’t understand what it is you’re trying to do. However, PHP differentiates upper case from lower case in variable name, and port Controller, COntroller and controller are different things. Besides, I think new \Controller\$controlador(); and new \controllers\$controlador; are not valid syntax.

3 answers

2

Well, I use this function. It is used as follows:

the function below goes in the autoload.php file

function funcaoCarregadora($ClassName){
    $pasta = ultimaPalavra($ClassName);
    include_once($pasta."/".$ClassName.".php");

}

function ultimaPalavra($string){
    $tmp = '';
    $char = '';
    for($i = strlen($string)-1; $i >= 0 ; --$i){
        $char = $string[$i];
        $tmp .= $char;
        if(ctype_upper($char)){
                break;
        }
    }

    return strrev($tmp);
}

then in the config.php file give a include of autoload.php

include_once('autoload.php');

config.php, has the security and redirect function.

and on all pages I give a config.php include

include_once('config.php');

and below that included I instate my classes

ready.

I hope I’ve helped!

Note: my classes are named as follows:

Filing cabinet

Myacclassedao.php

class

Myacclassed Minhaclassemodel Pathfinder

and View can be any name...

  • thanks for the help

1

I Googled and found the Solution

 if (is_readable($caminho)) {

            require $caminho;
            $controlador = ucfirst($controller);
//Adicionei a linha abaixo
      $namespace = "\\" . "controllers" . "\\" . $controlador; 
            $controller = new $namespace;

            if (is_callable(array($controller, $metodo))) {

                $metodo = $pedido->getMetodo();
            } else {
                $metodo = "index";
            }

            if (isset($parametro)) {
                call_user_func_array(array($controller, $metodo), $parametro);
            } else {
                call_user_func(array($controller, $metodo));
            }
        } else {
            header("Location:" . URL . "error");
        }

1

Your question and answer do not seem to compose with OOP code, if you can enlarge the example I can give wider answer, but for now, I think this example can judge.

You can create a simple autoload. I used stream_resolve_include_path to check if aruivo exists before importing.

spl_autoload_register( function( $file )
{
    if( stream_resolve_include_path( "{$file}.php" ) === false )
    throw new Exceptions( "O arquivo `{$file}` não existe." );

    include "{$file}.php";
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.