8
A friendly url of the type:
www.meusite.com.br/controlador/metodo/argumentos
Is treated in my class Request, where I "explode" the url
, separating it into segments, which are respectively, $controller
, $action
and $args
, who will be returned to my class Route, where the application’s "routing" is done. So far Ok.
Problem
The problem is that you simply add a subdirectory. (So I get all worked up, because I’m a beginner in PHP applications with MVC, mainly in the concepts of classes Route
and Request
).
Directory structure
What is "picking up" is that, as I described the process in the first paragraph, the application will work perfectly without the subdirectories Usr
and Adm
, having only the following hierarchy.
For the whole process is in accordance with the Router and the Request.
Class Router
class Router
{
public static function run(Request $request)
{
$controller= $request->getController();
$action= $request->getAction();
$args = (array) $request->getArgs();
$controller = 'Application\Controller\\' . ucfirst($controller);
$controller = new $controller();
if (!is_callable(array($controller, $action))) {
// Algum comando.
}
call_user_func_array(array($controller, $action), $args);
}
// Mais métodos
}
The above code is responsible for the inclusion of Controllers
and the call to methods based on what was returned by Request
. (this one doesn’t have much code, so I’ll just put the section that deals with the url
)
Request
public function __construct()
{
if (!isset($_GET["url"])) return false;
$segments = explode("/", $_GET["url"]);
$this->controller = ($controller = array_shift($segments)) ? $controller : "index";
$this->action = ($action = array_shift($segments)) ? $action : "main";
$this->args = (is_array($segments[0])) ? $segments : array();
}
Question
I would like to know what changes I should make to both codes so that a call to a controller, in any of the subdirectories, can be made successfully through the following format: url
www.meusite.com.br/nome_do_subdiretorio/controlador/metodo/argumentos
I apologize for the size of the question, but I tried to make it complete rsrs
Update
Controller Index
Exemplifying with this controller, it would be the standard, case a
url
came as follows:www.meusite.com.br/nome_do_subdominio/
namespace Application\Adm\Controller;
use MamutePost\Controller;
class Index extends Controller
{
// ... Métodos
}
Mamutepost is a folder inside Vendor where I put my "mini-framework", which I developed to work with MVC.
I remember the headaches I went through when I tried to develop my own route solution, until the moment I started working with symfony then much of my problems were gone, I’m not looking to go public, but if you want to know more about it, my advice. I once did a job where the customer complained of synfony being too big for something respectively simple, the advantage is that it allows the installation of the components separately. The reference for the route component http://symfony.com/doc/current/components/routing/introduction.html
– kabstergo
@kabstergo Thanks for the comment and I really believe that both symfony, or some other framework will handle the issue of routes very well, including I will take a look yes on the link, but for now I am looking for a solution without using framework. rs
– Thomerson Roncally
without problems, could post the code of a controller? but specifically I mean the
namespace
from it, considering the first example you gave I believe your controller would be located in the namespaceApplication\Controller\Nome
in the second case with the subfolders the namespace has changed toApplication\Pasta\Controller\Nome
?– kabstergo
@kabstergo I will update :)
– Thomerson Roncally