How to get base url inside controller in CakePHP - cakephp

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+

Related

Redirect on correct pagination index page after edit

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

Cakephp 2.5 Postlink hidding the model atribute name at the url

I am using a cakephp 2.5 postlink method inside a view file :
$tableRow['Model.modelatribute'] = $this->Form->postLink(
$data['Vehicle']['plate'],
array('controller'=>'somecontroller',
'action' => 'somemethod',
'Model.modelatribute' => base64_encode($data['Vehicle']['plate'])
),
array('confirm' => 'Look at vehicle '.$data['Vehicle']['plate'])
);
I would like not to show the model atribute name on the url bar. After clicking on the link and being redirected, the url shows :
somemethod/Model.modelatribute:vSpEeTIweQ%3D%3D
Can i hide the model atribute name, using postlink method o cakephp 2.5 ?
ThankĀ“s in advance
If you simply want to pass the value of Model.modelatribute as a parameter, just leave off Model.modelatribute in your routing array. If you want to pass the value without occouring in the url you can use the data option of postLink.
echo $this->Form->postLink(
$data['Vehicle']['plate'],
array(
'controller'=>'somecontroller',
'action' => 'somemethod',
base64_encode($data['Vehicle']['plate']) // Routing array without modelname
),
array(
'confirm' => 'Look at vehicle '.$data['Vehicle']['plate'],
'data' => array(
// Data option of postLink method
'Model.modelatribute' => base64_encode($data['Vehicle']['plate'])
)
)
);
Well, you put in there. If you don't want it to be there simply don't put it in the URL. You can alternatively put it into the body of the POST request.
If you're concerned that people can use the model name for something and you want to hide it for that reason, then it is a very bad approach called security through obscurity. Instead you want to ensure that you validate that only values you want are passed and processed. So check your incoming data, always, from everywhere.

how to set base url in cake php 3 in config/app.php file and get that url in all view

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.

Accessing actions with prefixes in cakephp

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", ... });

How to pass data in url using cakephp?

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

Resources