In Asp.Net MVC I have used helpers to specify paths to actions and controllers, like this:
var url = "#Url.Action("OrderAction", "OrderController")";
$.post(url, { name: name, prodno: prodno }, function(data) {
//Do stuff
});
Now I'm trying to learn Kohana and php, and I was wondering if there is a similar way to do it there? Or how else do I call a specific action method in a controller to post to it?
EDIT: Here's the routing in bootstrap.php if it helps:
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
You can use Reverse Routing to transform a route and optional arguments to the link...
echo Route::url('route_name', array('optional' => 'arguments'));
...could you provide an example of how I would use this to produce the call to OrderAction method in OrderController controller[?]
echo Route::url('default',
array('controller' => 'Order', 'action' => 'Action')
);
Related
I know POST method is pointing to add() in a controller as default in cakephp3. Is it possible to custom that and point POST method to index()? Something like below:
Router::connect(
'/test',
array(
'controller' => 'Test',
'action' => 'index',
'[method]' => 'POST'
)
);
Thanks to #ndm who made a very clear solution for my question.
One of my issue is that I have $routes->resources('Test'); which will disable #ndm's solutions. So first of all, I commented out the line $routes->resources('Test');.
Because of I'm not working on a solid project which is a temporary project for a narrowed purpose, so below code is working perfectly for me now.
Router::scope('/', function ($routes) {
$routes->setExtensions(['json']);
// $routes->resources('Test');
$routes->post(
'/test',
['controller' => 'Test', 'action' => 'add']
);
});
I am making small project in cakePHP v3.5, i am not able to use parametrized in appropriate way. Also in some cases if we want to pass optional parameter in url then how can i do?
CakePHP v3.5
$routes->get('test/testfn/:param1/:param2', ['controller' => 'Pages', 'action' => 'testfn']);
where
test: Controller,
testfn: Method of TestController,
param1: Parameter 1,
param2: Parameter 2
All i did to get params from url to TestController,
$this->request->getParam(param1)
$this->request->getParam(param2)
How can i list all parameters that i passed from routes to My Controller instead of single param step by step.
OR anyone have better options to do routing in cakePHP v3.5
Also I am confused about the paramterized routing principle of cakePHP3.4
so, in that case if anyone has some solution to cakePHP v3.4.
Please Help me.
Thanks
In config/routes.php
$routes->get(
'/api/test/*', ['controller' => 'Api', 'action' => 'check']
);
In controller
public function check($first=null, $sec=null) {
pr($params);
pr($sec);
die;
}
In Routes.php
$routes->connect('/users/:id/edit/:type', ['controller' => 'Users', 'action' => 'edit', ['id' => '\d+', 'pass' => ['id', 'type'], '_name' => 'edit-client']);
In this route in ID is user id and Type is user type two parameter pass in this route
if this route method is POST then ex - $this->request->getData('id'); and if this route method GET then ex - $this->request->getParam('id')
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_\-]*'
)
);
How do I pass extra parameters in the URL to KohanaPHP (version 3.1+) like so...
http://example.com/blog/edit/4000/3200
...where "blog" is the blog.php in the controllers folder, "edit" is a controller method, 4000 is parameter 1 which I can use for who wants to edit the record, and 3200 is parameter 2 which I can use for the record ID?
I get the blog and edit parts. The problem is the 4000 and 3200. When I run that URL, I get an error: "404 - Unable to find a route match blog/edit/4000/3200"
Am I forced to have to do something unusual with the .htaccess file, or pass the parameters as query params after a question mark?
This explains what to do:
http://kohanaframework.org/3.1/guide/kohana/routing
But essentially, I need to edit the application\bootstrap.php file and change this:
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index'
));
to this:
Route::set('default', '(<controller>(/<action>(/<param1>)(/<param2>)))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index'
));
And now in my blog.php controller file, I can now pass 2 parameters into the "action_edit" class method like so:
public function action_edit() {
$sParam1 = $this->request->param('param1');
$sParam2 = $this->request->param('param2');
$this->response->body('param1=' . $sParam1 . ' param2=' . $sParam2);
}
You need a route like this in your bootstrap.php:
Route::set('blog_edit', 'blog/edit/<param1>(/<param2>)')
array(
'param1' => '[[:digit:]]{1,}',
'param2' => '[[:digit:]]{1,}',
))
->defaults(array(
'controller' => 'blog',
'action' => 'edit',
));
Note: the "()" makes param2 optional.
In your controller you can access the parameters as method arguments or via the Request object:
class Controller_Blog
{
public function action_edit($param1, $param2)
{
// or
$param1 = $this->request->param('param1');
$param2 = $this->request->param('param2');
// [...]
}
}
I haven't tested this code but it should be very close to what you need.
Referenced from "Kohana PHP 3.0 (KO3) Tutorial Part 6"
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',
));