I find it easier and smarter to make such parameters follow a logic. Make the script smart and do not try to process anything that is not part of the system. ignore or set exception face. A responsive execution.
Like all your controllers start with (Controller_) everything you live after (Controller_) is what we want to pick up to find out which controller and which classes to call this URL.
Now establish that all Actions begin with (Actions_) what comes after (Action_) is what we take to know what action to call in our class and what class to raise.
Finally would come the parameters which would be all the parameters:
^(?:/)(?:[/]Action_([a-za-Z]+))(?:/)+(?:[/])?
<?php
preg_match('/^(?:[\/](?:Controller_([a-zA-Z]+)))(?:[\/]Action_([a-zA-Z]+))(?:[\/]([a-zA-Z0-9\/\-\_]+))+(?:[\/])?/', $onde, $matches);
//Analisamos o controller se tiver um controller fazemos algo se
//não houver deixa tudo para lá sem controller não há trabalho a se fazer
if((isset($matches[1]) == true) and ($matches[1] != null) ){
//Já que tem controller na variável $matches[1] seguimos em frente
// Verificamos se há action, pois se não houver paramos
// por aqui e retorna a ação padrão do referido controller
if((isset($matches[2]) == true) and ($matches[2] != null) ){
// Se estamos aqui é por que tem um action a ser executado
// Então proseguimos
//Agora verificamos se tem parâmetros, se não houver essa parte não é executada
// e é executada somente a ação padrão para o referido action
if((isset($matches[3]) == true) and ($matches[3] != null) ){
// Como estamos aqui quer dizer que há parâmetros na variável $matches[3]
// Aplicamos um explode() nele
$pedacos = explode("/", $matches[3]);
//Analisamos cada parâmetros e tomamos decisões
}
}
}
?>
This regular expression accepts:
/Controller_carrinho/Action_pagar/Param1/Param2/Param3/
/Controller_carrinho/Action_pagar/Param1/Param2/Param3
/Controller_carrinho/Action_pagar/Param1/
/Controller_carrinho/Action_pagar/Param1
/Controller_carrinho/Action_pagar
/Controller_carrinho/Action_pagar/
/Controller/
/Controller
The controller can be anything as long as it follows the logic
Controller_buy
Controller_sell
Controller_fazerpao
Controller_listarproducts
Controller_fazercafe
Controller_comerarroz
The variables are:
$Matches[1]
The name of the controller, only what comes after the underline
$Matches[2]
The name of the action, only what comes after the underline
$Matches[3]
All parameters provided they are letters with or without numbers and strokes and underline.
It does not matter if there is a trace at the end, because it is ignored and everything that does not fit into it is also ignored.
Observing:
In the case of missing a piece there are no errors the variable will have the null value.
I use this logic so I don’t have to use $_GET on my website and analyze something that is not part and does not follow the logic of the system.
I hope you help someone!
If you gave a substring to the first bar and then blew up the bar? It would suit you?
– JuniorNunes
Maybe.. but I’m looking for a solution that uses only regex, if any. Otherwise I’ll have to resort to other methods.
– Renan Cavalieri