Kohana 3.0 Using parameters in default route - php

I'm trying to use my index controller to create a url structure like this: mydomain.com/vehiclemake/vehiclemodel/vehiclemodelyear
I don't know how to alter the default route, or alter a duplicate that will work as intended. Every time I load the page once a make has been added to the url, it gives me a blank screen and the logs tell me it can't find a controller with the name of the vehicle make that was in the url. Below is the default and what I've tried.
Route::set('vehicle', '(/<make>(/<model>(/<model_year>)))')
->defaults(array(
'controller' => 'index',
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'index',
'action' => 'index',
));
I've tried looking for an answer here on stackoverflow but I haven't found a question that's similar to mine that has an answer.

I'm not sure if I understand your question right.
Anyway, you won't need the first / before .
Besides that, this structure will require a controller for every make and in that controller an action for every model. I think something like the code below will work for the uri: /ferrari/testarossa/1992
class Ferrari extends controller {
public function action_testarossa() {
// whatever you'd like to do
echo $this->request->param('model_year');
}
}
I think the default in routing always is, but I'm not sure.
1. controller
2. action
3. whateveryoudefine
4. whateveryoudefine
Hope this helps!

You must plan your routing so that one URL cannot be matched to 2 routes. I suggest:
Route::set('vehicle', '/<make>(/<model>(/<model_year>))')
->defaults(array(
'controller' => 'vehicle',
'action' => 'index',
));
Route::set('mainpage', '/')
->defaults(array(
'controller' => 'index',
'action' => 'index',
));
Route::set('default', '<controller>(/<action>(/<id>))', ['controller'=>'(index|user)'])
->defaults(array(
'controller' => 'index',
'action' => 'index',
));
Note that I removed 1 pair of brackets. And for default route correct controllers are: index and user.

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 !!!

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 ;)

CakePHP pages without controller in url

Is it possible to have pages route without the controller in the URL but still have other controllers work? Example:
Access pages like this: http://domain.com/about/
Instead of like this: http://domain.com/pages/about/
But still have access to http://domain.com/othercontroller/action/
Doing the following works for having the pages without /pages/ in the URL but if I try to access any other controller it doesn't work:
From: Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
To: Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'index'));
Is there a way to setup the Router so that it runs controller/action if it exists. If it does not it runs the pages controller/action?
I think the short answer is, no - it's not possible in the manner you're hoping*. Routing isn't really "logic" driven, so unless you can come up with a way to match the things you want in both respects you can't do "if controller exists, then _, else _" kind of thing.
*You could, however add each "page" as a row in your routes file. That would allow "about", "contact" ...etc to be accessed directly, while things that don't exactly match them are handled by the remaining routes.
I know I'm late, but here's my tip for someone looking for this.
In routes.php
foreach(scandir('../View/Pages') as $path){
if(pathinfo($path, PATHINFO_EXTENSION) == "ctp"){
$name = pathinfo($path, PATHINFO_FILENAME);
Router::connect('/'.$name, array('controller' => 'pages', 'action' => 'display', $name));
}
}
This will create a route for every ctp file in the View/Pages folder.
I actually solved this the opposite way from Dave's answer above, by adding a route for each controller, rather than each page. (I won't be adding new controllers very often, but I will be adding new content on a regular basis.)
// define an array of all controllers that I want to be able to view the index page of
$indexControllers = array('posts','events','users');
//create a route for each controller's index view
foreach ($indexControllers as $controller) {
Router::connect(
'/' . $controller,
array(
'controller' => $controller,
'action' => 'index'
)
);
}
//create a route to remove 'view' from all page URLs
Router::connect(
'/:title',
array(
'controller' => 'contents',
'action' => 'view'
),
array(
'pass' => array('title'),
'title' => '[a-z0-9_\-]*'
)
);

CakePHP route keeps going back to /

I am trying to connect /admin/ to a static page 'admin.ctp'.
I copied the pages controller for modification and copied the display function to admin_display. I also tried creating an admin_index function without parameters. My route looks like this at this moment:
Router::connect('/admin/', array('controller' => 'pages', 'action' => 'index', 'prefix' => 'admin'));
my admin_index function looks like this:
function admin_index() {
$page = 'admin';
$subpage = null;
$title_for_layout = 'Admin';
$this->set(compact('page', 'subpage', 'title_for_layout'));
$this->render('/admin');
}
I put admin.ctp in /views/pages/ and in /views/pages/admin/
Anyway. When I go to /admin/ it redirects me to /. But when I delete admin_index, it complains that the function does not exist, so I does look for it.
Help?
edit: Big correction, all my admin urls go back to /
edit2: resolved it, something with appcontroller :$
Create admin_index.ctp file in /views/pages/.
Remove $this->render('/admin'); from the admin_index function. (If you wanted to use admin.ctp, I think all you would have to do is to remove the / from the argument). There's no reason to render admin.ctp for admin_index, since it's natural for cake to render admin_index.ctp for admin_index function. You just don't gain anything by not doing that the cake way.
If it doesn't work, try
Router::connect('/admin/', array('controller' => 'pages', 'action' => 'index', 'prefix' => 'admin', 'admin' => true));
If you want to route /admin/*action* requests to pages controllers admin_action function, then add this line to routes.php:
Router::connect('/admin/:action/*', array('controller' => 'pages', 'prefix' => 'admin', 'admin' => true));
Router::connect('/admin/', array('controller' => 'pages', 'action' => 'index', 'admin')); would work with the standard pages controller

Kohana 3 - How do I make the Default Route pass arguments to the Controller's Action?

My controller action requires a parameter, but I can't get KO3's router to pass this parameter in the Default route. This sort of thing works with other routes. Here is an example to clarify...
In bootstrap.php...
Route::set('default', '(<controller>(/<action>(/<the_required_param>)))')
->defaults(array(
'controller' => 'DefaultController',
'action' => 'index',
'the_required_param' => 'some_default_value',
));
In controller file...
class Controller_DefaultController extends Controller
{
public function action_index($the_required_param)
{
echo 'value: ' . $the_required_param;
}
}
Another way to get the specified param would be:
$this->request->param('the_required_param');
You should also ensure you define your routes in order and ensure it matches what it's supposed to.
The problem was being caused by a greedy route (would match any uri), so the Router never reached the Default route. Below is an example for reference...
// The parenthesis caused this route to match any uri
Route::set('route-4-params', '(<controller>/<action>/<p1>/<p2>/<p3>/<p4>)');
Route::set('default', '(<controller>(/<action>))')
->defaults(array(
'controller' => 'default_controller',
'action' => 'index',
'the_required_param' => 'somevalue',
));

Categories