Created
August 31, 2015 16:45
-
-
Save anonymous/35f4cf4f9cf57aa11ffb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?PHP | |
class NoticiasController extends AppController | |
{ | |
public function read() | |
{ | |
$slug = $this->request->param('noticia'); //Pega o slug da notícia vindo do router... | |
$noticia = $this->Noticia->findBySlug($slug); //Pega a notícia baseada no slug | |
$this->set(compact('noticia')); //Envia pra view | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?PHP | |
//app/Lib/Routes/NoticiasRoute.php | |
App::uses('CakeRoute','Routing/Route'); | |
App::uses('ClassRegistry','Utility'); | |
class NoticiasRoute extends CakeRoute | |
{ | |
public function parse($url) | |
{ | |
$model = ClassRegistry::init('Noticia'); //Chama a instância da model Noticia | |
$url = parent::parse($url); //Pega os dados da url request | |
if(empty($url)) return false; //Se não bater o padrão do parse, apenas retorna false... | |
if($model->hasAny(array('slug'=>$url['noticia']))) return $url; //Busca se existe alguma notícia com aquele slug específico, se sim retorna a URL | |
return false; //Se chegar até aqui é pq não tem a notícia | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?PHP | |
//app/Config/routes.php | |
App::uses('NoticiasRoute','Lib/Routes'); //Chama a NoticiasRoute | |
Router::connect('/:noticia',array('controller'=>'noticias','action'=>'read'),array('noticia'=>'[a-zA-Z0-9-_]+','routeClass'=>'NoticiasRoute')); //E aqui usamos a NoticiasRoute |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment