there is a table for example user_rewrite_uri in mysql
| entity_id |user_id|user_url_key | ------------ ------------------- | 1 | 11 | myName.html | ------------- ------------------- | 2 | 12 | ohterName.html | ----------- ---------------------
All requests go to index.php or any router where we simply take the key in our case example.ru/user/myName.html that matches $key = 'myName.html' select it from the table. and we get the id in this case 11 and convert the request to something similar to example.ru/user/id/11 and then render. 404 error should be after all when nothing is found for not straightforward logic will confuse further. It is easier to perceive such a scheme url -> user key || id -> render || 404 url -> user key || id -> render || 404 url -> user key || id -> render || 404 and not url -> id -> 404 -> user key -> render || 404 url -> id -> 404 -> user key -> render || 404 in the latter case is really harder to catch 404 error.
update for concretization I will give an example of routing in my small personal project, you can see here https://github.com/lnroma/ncms/ completely .
/** * run application * @return bool */ static public function runApplet() { self::dispathEvent('run_application_before',array()); // это эвент для обсервера не обращаем внимания $params = self::getParams(); // берём параметра из url смотри ниже // алгоритм компиляции и проверки config'ов модулей self::$_controllObject = self::loadController($configModul,$params); self::dispathEvent('run_application_after',array()); return true; }
then take the parameters
/** * get params request * @return array */ static public function getParams() { $result = array(); $baseUri = parse_url(self::getBaseUrl()); $requestUri = $_SERVER['REQUEST_URI']; if(isset($baseUri['path']) && $baseUri['path'] != '/') { $requestUri = str_replace($baseUri['path'],'',$requestUri); } $params = explode('/',trim($requestUri,'/')); // тут был алгоритм генерации имени модуля, контроллера, action // generate params // $i=3 так как ранее первые 3 параметра это соответственно модуль, контроллер и action for ( $i=3; $i<count($params); $i++ ) { if( $i%2 != 0 ) { if(isset($params[$i+1])) { $result[$params[$i]] = $params[$i+1]; } } } // $result это array вида 'param' => '1', 'param2' => 2 и т.д. return $result; }
Next comes the controller instance
/** * call controller action * @param $objectController * @param $params * @return mixed * @throws Exception_Notfound */ static protected function _callControllerAction($objectController,$params) { $params = Core_App::getParams(); $action = $params['action'].'Action'; if(!method_exists($objectController,$action)) { throw new Exception_Notfound('Page not found'); } call_user_func(array($objectController,$action)); return $objectController; }
Next comes the execution of the requested action in which the array of parameters will actually be available.
Core::app()->getParams()['userKey']; then we already implement a sample with a base, I have them 2-ve mongo and mysql , since I must have the integrity defined for the url I use mysql . The application has a functional on alisam for pages and modules. The mechanism to madness is simple. And it is built mainly on the observatory modules.
'router_load' => array( 'observer' => 'Pages_Model_Observer', 'method' => array( 'aliasForPage', 'aliasMenu' ) ),
This is a piece of the module aliasForPage It says that after loading the router, run the aliasForPage and aliasMenu in the Pages_Model_Observer class and Pages_Model_Observer aliasForPage ;
/** * router for page * @param $data * @return array */ public function aliasForPage($data) { /** @var Driver\Manager $connect */ $connect = Core_Model_Mongo::getConnect(); $man = new MongoDB\Driver\Query( array( 'key' => $this->_getPath() ) ); $collection = $connect->executeQuery(Config_Db::getConf()['mongodb']['db'] . '.pages', $man); if (count($collection->toArray())) { $data = array( 'controller' => 'pages', 'controllerName' => 'view', 'action' => 'index', 'id' => $this->_getPath() ); } return $data; }
In fact, the method checks whether this url is in byte, if not, then we send the algorithm to run further, if there is, then we say that it should start the next module, with the id parameter, it indicates hash Mongo db. After that, the controller that already generates the page views is started based on the data obtained by means of id.
$_GETyourself, then its meaning is lost, you can get out of the bugs, I sent it?test=mytestand in php I received$_GET['test'] => don't my test- Naumov