Define dynamic actions/slugs with Routers in PhalconPHP - php

I have a website made in PHP Phalcon, and it has several controllers.
Now I need this website to receive words in the URL (example.com/WORD) and check if they exist in the database, but I must continue to support other controllers like example.com/aboutme.
After few hours of trying different methods and searching online I cannot find a way to accomplish this task. The closer intent was creating a Route to redirect non-existing actions to a new controller, but I cannot make this solution work.
Can you think on a solution that may work and share the code/idea? I am not adding any code because I could not get to do anything useful.

This should not be a problem at all. Here are sample routes:
// Default - This serves as multipurpose route definition
// I can use it to create non pretty urls if I don't want to define them.
// Example: profiles/login, profiles/register - Don't need those pretty :)
$router->add('/:controller/:action/:params', ['controller' => 1, 'action' => 2, 'params' => 3]);
$router->add('/:controller/:action', ['controller' => 1, 'action' => 2]);
$router->add('/:controller', ['controller' => 1]);
// Product page - www.example.com/product-slug-here
$router->add('/{slug}', 'Products::view')->setName('product');
// Blog
$router->add('/blog/{slug}', 'Blog::view')->setName('blog');
$router->add('/blog', 'Blog::index')->setName('blog-short');
// Contacts - Pretty urls in native website language
$router->add('/kontakti', 'Blabla::contacts')->setName('contats');

After all, rules did not worked, so I came out with a workaround. I am catching the the exception raised when a controller don't exist and running my code there. So far the only issue is the bitter taste of contributing to spaghetti code :-(
try{
$application = new Application($di);
echo $application->handle()->getContent();
}
catch(\Phalcon\Mvc\Dispatcher\Exception $e)
{
$word = substr($e->getMessage(), 0, strpos($e->getMessage(), "Controller"));
// RUN ESPECIFIC CODE HERE
}

I see that the question is very old, but I found it while searching for a solution to the same problem, and what worked for me is adding the '/' for the controllers route:
// Page Not Found - 404
$router->notFound(
[
'controller' => 'index',
'action' => 'pageNotFound',
]
);
// Blog page - www.example.com/blog-slug-here
$router->add(
'/{slug}',
[
'controller' => 'BlogPost',
'action' => 'index',
]
)->setName('Blog-post');
$router->add('/:controller/:action/:params',
[
'controller' => 1,
'action' => 2,
'params' => 3
]
);
$router->add('/:controller/:action',
[
'controller' => 1,
'action' => 2
]
);
$router->add('/:controller/',
[
'controller' => 1
]
);
$router->add(
'/',
[
'controller' => 'index',
'action' => 'index',
]
);
This way I can have a blog post URL like www.example.com/admin and an admin area as www.example.com/admin/

Related

Action now allowed with Plugin Authake in CakePHP

I've a small problem with Rules in Authake Plugin and I want to know if anyone passed for the same problem or can help me.
Authake Plugin is really nice and saved me a lot of work.
My problem is with the rules, I want to allow user to access the following actions:
/authake/user/*
/register
/login
/logout
/lost-password
/verify(/)?*
/pass(/)?*
/profile
/denied
/doencas
/desordens/index
/desordens/view
/profissionais/index
/profissionais/view
/nacionais/index
/nacionais/view
/instituicos/index
/instituicos/view
The permissions works for the most of actions, except for
/profissionais/index
/profissionais/view
/instituicos/index
/instituicos/view
I have configured the routes in routes.php:
Router::connect('/register', array('plugin'=>'authake', 'controller' => 'user', 'action' => 'register'));
Router::connect('/login', array('plugin'=>'authake', 'controller' => 'user', 'action' => 'login'));
Router::connect('/rest_login', array('plugin'=>'authake', 'controller' => 'rest_user', 'action' => 'login'));
Router::connect('/logout', array('plugin'=>'authake', 'controller' => 'user', 'action' => 'logout'));
Router::connect('/lost-password', array('plugin'=>'authake', 'controller' => 'user', 'action' => 'lost_password'));
Router::connect('/verify/*', array('plugin'=>'authake', 'controller' => 'user', 'action' => 'verify'));
Router::connect('/pass/*', array('plugin'=>'authake', 'controller' => 'user', 'action' => 'pass'));
Router::connect('/profile', array('plugin'=>'authake', 'controller' => 'user', 'action' => 'index'));
Router::connect('/denied', array('plugin'=>'authake', 'controller'=>'user', 'action'=>'denied'));
Router::connect('/doencas', array('plugin'=>'', 'controller'=>'desordens', 'action'=>'index'));
Router::connect('/profissionais', array('plugin'=>'','controller'=>'profissionais'));
Router::connect('/desordens', array('plugin'=>'','controller'=>'desordens'));
Router::connect('/instituicos', array('plugin'=>'','controller'=>'instituicos'));
Router::connect('/sinonimos', array('plugin'=>'','controller'=>'sinonimos'));
Router::connect('/dadosNacionais', array('plugin'=>'','controller'=>'dadosNacionais'));
Router::connect('/referencias', array('plugin'=>'','controller'=>'referencias'));
Just the actions of that 2 controllers (instituicos and professinais) don't work.
I search in many sites, I looked at the MySQL table(Authake_rules), the files in Cake many times and I can't find some reason to this happen.
And I have others 2 actions I didn't put on the list of allowed actions and this actions is allowed for public access.
I don't know if this is a Bug in Plugin or I forget something, I have read many times the Docs in the GitHub project: https://github.com/mtkocak/authake looking for something but I didn't find anything to help me with this.
Anyone can help me please?
After hours trying i discover a possible solution, i just put a new route for each controller with a alias, for example in Profissionais Controller i put:
/profissionais/index
/profissionais/view/
/especialistas
/especialistas it's a alias created for the Profissionais Controller, with this everything works fine!! I create one alias for each others controllers and now its work !!!

Does Cakephp Auth can be use even in other controller?

Recently, I've been studying cake, I've seen the auth library which said to be will take care of the access control over your app, but, it seems like, you can't initialize or even use this auth library when you're not in the 'UsersController', i did not want that, what if it has some admin part wherein i want the URI to be admin/login, or just simply /login, i've been scratching my head over this one, please help.
Another question, why it seems like the functionality of the '$this->redirect' is not effective when i'm putting this one at any method that contains nothing but redirection, or even in the __construct()?
thanks guys, hoping someone could clearly explain to me those things.
you can use the Auth component inside any controller in the application. If you want it will only effect with the admin section then you can add condition in the beforeFilter funciton in you application AppController on Auth initialization like.
// for component initialization.
public $components = array(
'Auth' => array(
'authenticate' => array(
'userModel' => 'Customer', // you can also specify the differnt model instead of user
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
}
and you can bind this on the admin routing like
function beforeFilter(){
// only works with admin routing.
if(isset($this->request->params['prefix']) && ($this->request->params['prefix'] == 'admin')){
$this->Auth->loginRedirect = array('admin' => true, 'controller' => 'pages', 'action' => 'index');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => true);
$this->Auth->loginAction = array('admin' => true, 'controller' => 'customers', 'action' => 'login');
}
}
If you're using cake 2.3.x or later then make sure you have specified the redirect action in correct format like.
return $this->redirect('action_name'); // you can also specify the array of parameters.

CakePHP pagination on custom route

I obviously have a fundamental misunderstanding of how pagination works in CakePHP.
I have the following route set up which shows all posts in a category:
Router::connect('/:parent/:category',
array('controller' => 'posts', 'action' => 'viewCategory'),
array('parent' => '[a-z0-9-]+', 'category' => '[a-z0-9-]+'));
The pages work fine, however the pagination helper is outputting the wrong links for pagination.
I'm using $this->Paginator->numbers().
It's outputting links in this format: mysite.com/posts/viewCategory?page=2
rather than like this: mysite.com/parent-category/sub-category?page=2.
I've tried adding the following route after the first one and it still doesn't work:
Router::connect('/:parent/:category/:page',
array('controller' => 'posts', 'action' => 'viewCategory'),
array('parent' => '[a-z0-9-]+',
'category' => '[a-z0-9-]+',
'page' => '[0-9]+'));
For reference, my pagination options set in my view are as so:
<?php $this->Paginator->options(
array('url' =>
array('controller' => 'posts', 'action' => 'viewCategory')
)); ?>
What am I doing wrong here?
You are setting the url yourself
This is your paginator options call:
<?php
$this->Paginator->options(array(
'url' => array(
'controller' => 'posts',
'action' => 'viewCategory'
)
));
?>
Where you are overriding the current url - and explicitly requesting that the paginator uses the the '/posts/viewCategory' url (with no arguments) as it's base url.
Just don't define the url
Simply don't call options and the helper will use the current url - that should mean that if the current url is:
/parent-category/sub-category
Then page 2 will be (assuming you are using the paramType option to use GET arguments rather than named parameters):
/parent-category/sub-category?page=2
If that's not the case there's information missing from the question; it's important to distinguish between "vanity routes not being used" and "the url is not equivalent (the current situation).
Just had a battle fixing something similar and came across this post. Though old, but I think my answer might save someone the time I had to spend fixing it.
Basically, what you need to do is call the Paginator->options() before Paginator->numbers(), thus:
$this->Paginator->options(
array(
'controller' => 'parent-category',
'action' => 'sub-category'
)
);
Though the controller and action do not exist, it just tricks CakePHP to use them "AS IS", since the reverse routing isn't working!
And for those (like me), who want have set up a route similar to
Router::connect(
'/go/page:id',
array(
'controller' => 'blog',
'action' => 'paginated'
)
);
There might be difficulty setting up the Paginator options. This, however, worked for me:
$this->Paginator->options(
array(
'controller' => 'go',
'action' => '/'
)
);
I guess you know why it worked ;)

Kohana 3.2 - Routing questions

Firstly, Kohana's documentation is terrible, before people go "read the docs" I have read the docs and they don't seem to make much sense, even copying and pasting some of the code doesn't work for some things in the documentation.
With that in mind, I have a route like so:
//(enables the user to view the profile / photos / blog, default is profile)
Route::set('profile', '<userid>(/<action>)(/)', array( // (/) for trailing slash
"userid" => "[a-zA-Z0-9_]+",
"action" => "(photos|blog)"
))->defaults(array(
'controller' => 'profile',
'action' => 'view'
))
This enables me to go http://example.com/username and be taken to the users profile, http://example.com/username/photos to be taken to view the users photos and http://example.com/username/blog to view the blog.
If somebody goes http://example.com/username/something_else I want it to default to the action view for the user specified in <userid> but I can't seem to find any way of doing this.
I could do it like this:
Route::set('profile', '<userid>(/<useraction>)(/)', array(
"userid" => "[a-zA-Z0-9_]+",
"useraction" => "(photos|blog)"
))->defaults(array(
'controller' => 'profile',
'action' => 'index'
))
then in the controller do this:
public function action_index(){
$method = $this->request->param('useraction');
if ($method && method_exists($this, "action_{$method}")) {
$this->{"action_{$method}"}();
} else if ($method) {
// redirect to remove erroneous method from url
} else {
$this->action_view(); // view profile
}
}
(it might be better in the __construct() function but you get the gist of it.)
I'd rather not do that though if there is a better method available (which there really should be)
I think the answer might be in the regex but the following does not work:
$profile_functions = "blog|images";
//(enables the user to view the images / blog)
Route::set('profile', '<id>/<action>(/)', array(
"id" => "[a-zA-Z0-9_]+",
"action" => "($profile_functions)",
))->defaults(array(
'controller' => 'profile'
));
Route::set('profile_2', '<id>(<useraction>)', array(
"id" => "[a-zA-Z0-9_]+",
"useraction" => "(?!({$profile_functions}))",
))->defaults(array(
'controller' => 'profile',
'action' => 'view'
));
although it does match when nothing is after the ID.
I would set up the route like this:
Route::set('profile', '<userid>(/<action>)(/)', array(
"userid" => "[a-zA-Z0-9_]+",
"action" => "[a-zA-Z]+"
))->defaults(array(
'controller' => 'profile',
'action' => 'index'
))
And then in the controllers before() method:
if(!in_array($this->request->_action, array('photos', 'blog', 'index')){
$this->request->_action = 'view';
}
Or somethig similiar, just validate the action in the controller...
EDIT:
This could also work:
if(!is_callable(array($this, 'action_' . $this->request->_action))){
$this->request->_action = 'view';
}

CakePHP Router::connect() aliases?

Is it possible in CakePHP to have URL aliases in routes.php? Or by what other means can achieve something equivalent:
Lets assume I have some paginated views. Among the possible orderings there are particular ones I want to bind to a simple URL. E.g.:
http://example.com/headlines => http://example.com/posts/listView/page:1/sort:Post.created/direction:desc
http://example.com/hottopics => http://example.com/posts/listView/page:1/sort:Post.view_count/direction:desc etc.
How do I add parameters to a Router::connect()? Pseudo code:
Router::connect('/'.__('headlines',true),
array(
'controller' => 'posts',
'action' => 'listView'
'params' => 'page:1/sort:Post.created/direction:desc',
)
);
Note that the Router "translates" a URL into Controllers, Actions and Params, it doesn't "forward" URLs to other URLs. As such, write it like this:
Router::connect('/headlines',
array(
'controller' => 'posts',
'action' => 'listView'
'page' => 1,
'sort' => 'Post.created',
'direction' => 'desc'
)
);
I don't think '/'.__('headlines', true) would work, since the app is not sufficiently set up at this point to translate anything, so you'd only always get the word in your default language back. Also, you couldn't switch the language anymore after this point, the first use of __() locks the language.
You would need to connect all URLs explictly. To save you some typing, you could do this:
$headlines = array('en' => 'headlines', 'de' => 'schlagzeilen', ...);
foreach ($headlines as $lang => $headline) {
Router::connect("/$headline", array('controller' => ..., 'lang' => $lang));
}
That will create a $this->param['named']['lang'] variable, which you should use in the URL anyway.
Yes, it is possible... Bootstrap.php loads before routes so if you set there something like:
session_start();
if(isset($_SESSION['lng'])){
Configure::write('Config.language', $_SESSION['lng']);
}
...and in your app controller in beforeFilter:
$language = 'xy';
Configure::write('Config.language', $language);
$_SESSION['lng'] = $language;
So initial page render you prompt for language, redirect to xy.site.com or www.site.com/xy whatever you prefer. Now second render will change $language and on page links and set $_SESSION['lang']...
All router links like:
Router::connect(__('/:gender/search/:looking_for/*'), array('controller' => 'users', 'action' => 'search'));
will become:
Router::connect(__('/:gender/trazi/:looking_for/*'), array('controller' => 'users', 'action' => 'search'));
or:
Router::connect(__('/:gender/suche/:looking_for/*'), array('controller' => 'users', 'action' => 'search'));
100% tested, works in CakePHP 2.2. Also further improvement is possible if you put subdomain/language url parser in the bootstrap itself...

Categories