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')
Related
I'm using CakePHP 3 and I want to paginate my users.
However when I click on the second page, the URL looks like /users?page=2 and I expect : /users/2.
I created this route in routes.php :
$routes->connect('/users/:page', ['controller' => 'users', 'action' => 'index'], ['page' => '[0-9]+']);
And in Users/index.ctp before the "prev" button I put :
<?php
$this->Paginator->options([
'url' => [
'controller' => 'users',
'action' => 'index'
]
]);
?>
Now when I click on page 2 for example, /users/2 opens and I got this error message (RuntimeException) :
Unable to locate an object compatible with paginate.
Did I miss something or where I made a mistake ?
Thanks for your help.
The PaginatorHelper has built in the url format, i.e. to use ?page=n. It will also do sorting such as users?page=2&sort=user_id&direction=asc. Your format of /users/{page} does not handle sorting.
If your REALLY want to stick to /users/{page} you'll have to override PaginatorHelper.
try this
in side your controller with paginator component . It works for me
$this->Paginator->paginate('Users')
for custom urlenter code here
u need to implement index action as
public function index($page = null){
$this->Paginator->settings = ['limit' => 15, 'page' => $page];
$this->set('users', $this->Paginator->paginate('Users'));
}
I have the following route:
Router::connect('/admin/login/:to',
array('admin'=>true,'controller'=>'users','action'=>'login'),
array(
'to' => '[A-Za-z0-9\._-]+',
'pass' => array('to')
));
Which basically passes a string/int with the login url. But it no longer uses the named parameter of to. So for example instead of getting: /admin/login/to:1AB I get /admin/login/1AB
How do I keep the named parameter but still alter the routing to remove the users bit from the url? I've tried: '/admin/login/to::to' but that seems rather sloppy...
you could find passed parameter's name in "$this->data" in your controller.
in your example: $this->data->to has the same value you put in your url.
Remove that route. Why do you have that route when you want the named parameter?
Edit: if so:
Router::connect(
'/admin/login/*',
array(
'admin' => true,
'controller' => 'users',
'action' => 'login'
)
);
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')
);
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',
));