im still noob with cakephp. I want to access an action with a prefix but im being redirected to my current view. how can i do this? Example:
ihave a function like below:
function admin_getID()
{
some codes here...
}
in my link. i accessed it using this html helper:
$this->Html->url(array('action'=>'getID', 'admin'=>true))
note that currently i dont have any prefix and i want to access the action with a prefix.
the url will be used in jQuery.ajax's URL so in the jquery,
jQuery.ajax({
...
url:"Html->url(array("action"=>"getID", "admin"=>true))?>",
...
});
Thank you!
Since in your file core.php you are using the same prefix, for example:
Configure::write('Routing.prefixes', array('admin'));
You should use:
echo $this->Html->link('link', array('action' => 'getID', 'admin' => true));
This will generate the link /admin/{your_controller}/getID.
For the same Controller, but if you want to display to another controller, you must include the controller parameter in the array.
If you're not using the directive Routing.prefixes as I said above, simply add the admin_getID parameter in action value.
I think you are talking about routing. So for example, if you want to define actions for admin like:
admin_index
admin_edit
admin_view
adn have them accessible by
example.com/admin/index
example.com/admin/edit
example.com/admin/view
This is called routing in CakePHP. You can see how this is done here:
http://book.cakephp.org/1.3/en/view/948/Defining-Routes
Update
You can just do this:
<?php echo $this->Html->link('link', array('controller' => '{CONTROLLER_NAME}', 'action' => 'getID', 'admin' => 'true')); ?>
UPDATE 2
You are not echoing your URL. You need to do this:
jQuery.ajax({ ... url:"<?php echo $this->Html->url(array("action"=>"getID", "admin"=>true)); ?>", ... });
If you are not using PHP to render your jQuery, you cannot use cake to generate your URL, you will have to do it manually:
jQuery.ajax({ ... url:"/admin/CONTROLLER/getID", ... });
Related
I am trying to edit(edit.cto) on page 2 but after edit it's always redirecting to the index page 1.
How can i redirect on right pagination?
expected->
https://localhost/erp-development/EmployeeMonthlySalaries/index/page:2
getting -> https://localhost/erp-development/EmployeeMonthlySalaries/index/
You can do that using session.
For example in index controller set:
$this->request->session()->write('referer', ($_SERVER['REQUEST_URI'])); //cakephp 3
$this->Session->write('referer', ($_SERVER['REQUEST_URI'])); //cakephp 2
Then in edit controller:
return $this->redirect($this->request->session()->read('referer')); //cakephp 3
return $this->redirect($this->Session->read('referer')); //cakephp 2
Firstly I would recommend not to use named parameters, as they are a CakePHP-only feature and not standard.
I would recommend to use regular GET parameters using:
'Paginator' => array(
'limit' => 15,
'paramType' => 'querystring'
),
in your components definition.
I would then build all URLs using a custom-defined makeUrl() function, or overriding HtmlHelper::link() one, so that they all contain the querystring received.
Your edit action will then have the URL: /controller/edit/?page=2
and you edit action will know how to build the redirect URL /controller/index?page=2
You can simply replace below redirect code where you write your redirect code in controller:
<?php $this->redirect($this->referer()); ?>
I am using CakePHP 2.6.7. I want to get the following url:
www.mydomain.com/OrderFromReseller/82BC1562-22F9-4326-8B4B-370129710E8C
I try as:
$resellerURL = $this->webroot . 'OrderFromReseller/' . $this->request->data['Reseller']['api_key'];
But when I echo the value of $resellerURL it echos http://orderfromreseller/82BC1562-22F9-4326-8B4B-370129710E8C.
Is there any alternative of $this->webroot to get base url properly inside cakephp?
You want to use Router::url() to get the full URL. For example to get the full web address for the homepage:-
Router::url('/', true);
You can pass any router array as the first parameter to get the full URL as long as you pass true as the second parameter. For example:-
$url = Router::url(
['controller' => 'pages', 'action' => 'display', 'test'],
true
);
It work for me in cakephp 3.5 please follow the below code
1) add this code in the top of the controller below namespace App\Controller;
use Cake\Routing\Router;
2) $baseUrl = Router::url('/', true);
This is the best and simple way to get the base url in cakephp 3+
i would like to set base 'url' like "codeigniter" and that can be access in all views. I want to get full url like "http://localhost/somepath"
You probably don't need to do that, as the Helpers in Cake do that automatically.
For example, if your app is under http://localhost/somepath, creating a link like this
echo $this->Html->link('home', '/');
will automatically point to http://localhost/somepath
Links to actions work the same way:
echo $this->Html->link('login', ['controller' => 'Users', 'action' => 'login']);
will automatically point to http://localhost/somepath/Users/login
And if you do need to get the url anywhere else than in a view, you can do it like this:
use Cake\Routing\Router;
$path = Router::url('/', true);
In CakePHP 3 You can define "BASE_URL" constant in
yourAppFolder/config/paths.php as
define('BASE_URL', 'www.yoursite.com/');
and use BASE_URL anywhere in your project.
I wanted to now if there is a way to pass a simple string inside url and cakephp some how put it inside one of the inputs of my form without any code writing on view side?
I tried calling this->set("bla","bla"); the field name is bla
but nothing changed in view
As I understood the question you want to have something like this:
in the url you have something like:
http://yourserver.com/controller/action?search=string
and you want to put this "string" in the search field, right?
Then let's imagine that your field's name is data[search_field]. (I am skipping the model, because for my example it's not needed, but it's possible the name to be data[Model][search_field]).
Then in your controller's action you have to do following:
$this->data['search_string'] = $this->params['url']['search'];
You can pass values in the url by using html helper. Try:
echo $this->Html->link('View Some Page', array(
'controller' => 'yourController',
'action' => 'view',
1, // id
'?' => array('yourField' => 'valueToPass'))
);
I'm working on upgrading my project from CakePHP 1.2 to 1.3. In the process, it seems that the "magic" routing for plugins by which a controller name (e.g.: "ForumsController") matching the plugin name (e.g.: "forums") no longer automatically routes to the root of the plugin URL (e.g.: "www.example.com/forums" pointing to plugin "forums", controller "forums", action "index").
The error message given is as follows:
Error: ForumsController could not be found.
Error: Create the class ForumsController below in file: app/controllers/forums_controller.php
<?php
class ForumsController extends AppController {
var $name = 'Forums';
}
?>
In fact, even if I navigate to "www.example.com/forums/forums" or "www.example.com/forums/forums/index", I get the same exact error.
Do I need to explicitly set up routes to every single plugin I use? This seems to destroy a lot of the magic I like about CakePHP. I've only found that doing the following works:
Router::connect('/forums/:action/*', array('plugin' => 'forums', 'controller' => 'forums'));
Router::connect('/forums', array('plugin' => 'forums', 'controller' => 'forums', 'action' => 'index'));
Setting up 2 routes for every single plugin seems like overkill, does it not? Is there a better solution that will cover all my plugins, or at least reduce the number of routes I need to set up for each plugin?
I guess, that topic Configuration-and-application-bootstrapping covers that:
App::build(array(
'plugins' => array('/full/path/to/plugins/', '/next/full/path/to/plugins/')
));
Also take a look at this ticket: http://cakephp.lighthouseapp.com/projects/42648/tickets/750-plugin-route-problem-when-acl-and-auth-components-used#ticket-750-5 (Cake 1.3 had removed magic plugin routes).
You don't have myplugin_app_controller.php in your /app/plugins/myplugin directory.
Just create a file containing following:
<?php
class MypluginAppController extends AppController {
}
?>
And you will have all your plugin's features. :)