0
I’m trying to give a restructured in a personal microframework, before not used much standard in the structure of the project, now I played all my back-end in the folder source
, and I’m using namespaces
to separate the code. I’m having trouble doing the autoload
, want to give a new Crontrolador()
without first having to use the use \Controllers\Controlador
.
I never really understood that part so I came to ask for help, currently in my class Core I have the following method which is responsible for making some validations and calling the controller:
private function addControllerAndModel($class, $method, $page = false)
{
$class = ucfirst($class);
$controller = "{$class}Controller";
if(file_exists(__DIR__."/../../Controllers/{$controller}.php")) {
echo "Controller existe<br>";
if($page)
$this->importRels(Relationships::get('*'));
$this->importRels(Relationships::get($class));
$instance = new $controller();
if($page) {
if(count($_POST)) {
if($_POST['token'] === Session::get('token') or in_array($_POST['token'], Settings::get('allowsAjax'))) {
Session::set('token', false);
Saved::create();
$method = "{$method}Posted";
}
else {
Errors::display('Token inválido', DOMAIN);
exit();
}
}
else {
if(!Session::get('token'))
Session::set('token', md5(crypt(time(), '$1$rasmusle$')));
$method = "{$method}Deed";
}
}
$instance->{$method}();
// else
//Errors::display("Método não encontrado [{$controller}/{$method}]");
}
//else
//Errors::display("Controlador não encontrado [{$controller}]");
echo $this->controller . " / " . $this->method;
}
Briefly, I see if there is the requested controller file, if there is I import the Models related to that controller, and then I prompt the controller, after that I have some security controls, and finally I call the desired method.
Currently error on line 126 ($instance = new $controller();
):
Fatal error: Uncaught Error: Class 'Homecontroller' not found in /home/Leonardo/www/tcc/newframework/source/Kernel/Kernel/Core.php:126
Follow the folder structure of my project:
Project
--binary
--public
----js
----css
----librarys
----views
--source
----Controllers
------MeuControlador.php
----Kernel
------Kernel
--------Core.php
----Models
------MeuModel.php
--settings
--third
--trials
The mentioned function is present in Core.php, and the controllers are two levels behind and in the Controllers folder. I do not understand how to make such autoloader with namespaces, so the only attempt I have is this method above.
An example of a controller:
<?php
/**
* Created by PhpStorm.
* User: leonardo
* Date: 17/09/16
* Time: 14:52
*/
namespace Controllers;
class HomeController extends Kernel\Control\Controller
{
public function startDeed()
{
echo "Hello World!!";
}
}
It is possible to give new HomeController()
undeclared use Controllers\HomeController
? I tried using:
$instance = \Controllers\{$controller}();
But it didn’t work :(
It worked, I don’t know how I got that simple detail kk
– Leonardo