In CakePHP 3.1 I am redirecting to login page after a database update and I want to pass back the email so that form input is already populated.
I can do it the standard way with using the controller and action in the redirect.
Instead I am using just a url string
return $this->redirect('/login',['?' => ['email' => $email]]);
This gives me the error Unknown status code
The redirect method expects a status code as the second parameter. You will need to provide and array-based URL as the first parameter or append the query var to the current string.
return $this->redirect('/login?email=' . $email);
return $this->redirect([
'controller' => 'Users',
'action' => 'login',
'?' => [
'email' => $email
]
]);
Related
I am trying to insert an entire api response into a MySQL table using Laravel Eloquent but I am getting 'Array to string conversion' error. How do I solve this?
Please note, it is compulsory that I save the entire API response.
My API call
$response = Curl::to($url.$request->account_number)
->withData($data)
->asJson(true)
->get();
My Query
TransactionLog::create([
'payer' => $request->payer,
'amount' => $request->amount,
'phone' => $request->phone,
'response' => $response
]);
Looks like you using ixudra/curl ?
With asJson(true) you will receive json_decode response, so if your plan is to save raw json response in the database you'll have to json_encode it like:
TransactionLog::create([
'payer' => $request->payer,
'amount' => $request->amount,
'phone' => $request->phone,
'response' => json_encode($response)
]);
the \kartik\grid\EditableColumn widget has a parameter called ajaxSettings where you may override the parameters passed with the ajax request to the server. What i want to do is to dynamically pass the selected rows ids together with the value coming from the popover to the server. I manage to do that passing static parameter coming from a php array in compile time like so
Editable::widget(['name' => 'publishDate', 'ajaxSettings' => ['ids' => [1,2,3]]])
but It seems that i cannot use a jquery selector there to grab the ids of the selected columns like so
Editable::widget([
'name' => 'publishDate',
'ajaxSettings' => [
'ids' => '$("#books-grid").yiiGridView("getSelectedRows")'
]
])
Maybe you want to try creating a variable outside the Editable::widget([ like this:
var arrayIds = $("#books-grid").yiiGridView("getSelectedRows");
And then assign it to the widget:
Editable::widget([
'name' => 'publishDate',
'ajaxSettings' => [
'ids' => arrayIds
]
])
Hope this helps,
Leo.
I'm working on a project rebuild using CakePHP, and following the new Authentication documentation here:
http://book.cakephp.org/3.0/en/controllers/components/authentication.html
From what I'm reading, Cake3 uses the userModel='User' by default, but it has the option to set it to whatever you want. In my case, I have all the auth data in the 'Account' model (i.e. userModel => 'Account').
So, in my Account Entity, I added the following code:
protected function _setPassword($password)
{
return (new DefaultPasswordHasher)->hash($password);
}
Additionally, in my accounts table, my 'passwd' field is set to varchar(255) [I've read that's required for some reason].
When I use my default baked 'add' and 'edit' methods, the password is stored in plain text, and not hashed. The ONLY way I've found to get around this is to create a custom method in the AccountsTable class then call it using this kludge:
$this->request->data['passwd'] = $this->Accounts->hashPassword($this->request->data['passwd']);
My Auth component looks like this...
$this->loadComponent('Auth', [
'loginAction' => [
'controller' => 'Accounts',
'action' => 'login'
],
'authError' => 'Unauthorized Access',
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'username',
'password' => 'passwd'
],
'userModel'=>'Accounts'
]
]
]);
Is there a way to do this without dinking around with the raw request data?
Your mutator is named wrongly, the convention for mutators is _set followed by the camel cased field/property name. So since your field name is passwd, not password, it has to be named _setPasswd instead.
protected function _setPasswd($password)
{
return (new DefaultPasswordHasher)->hash($password);
}
See also Cookbook > Entities > Accessors & Mutators
I try to send custom data using $.get from jquery but cakephp 3 does not recognize the variables.
Here is my function in controller:
public function view($cat,$id,$page){
$comments = $this->Comments->find('all')->where(['category =' => $cat, 'AND' => ['category_id =' => $id]])->order(['comments.created' => 'DESC'])
->contain([
'Reports' => function($q){return $q->where(['user_id =' => $this->Auth->user('id')]);},
'Chars' => function($q){ return $q->select(['id','name','class','race','level','guild_id', 'user_id'])
->contain(['Guilds' => function($q){ return $q->select(['id','name']);
}]);
}])->limit(3)->page($page);
if($cat == 'Videos'){
$subject = $this->Comments->Videos->get($id);
}
$category = $cat;
$this->set(compact('subject','comments','category'));
}
}
And here's the .js
$('.more').click(function(){
$.get($(this).attr('href'),{cat:'Videos',id:'44',page:'2',function(data){
$('.coms').empty().append(data);
});
return false;
});
And the link:
<?= $this->Html->link('More', ['controller' => 'Comments','action' => 'view'], ['class' => 'btn btn-primary more']) ?>
The fix values in the .js is for the test, it works if I send the data in the link with $.get(href) but I want to know how to pass custom data in the $.get request.
Thank you for your help
CakePHP doesn't magically map query string parameters to method arguments, that's not even supported by routes.
You can access query string paramters via the request object
$this->request->query('cat');
See also http://book.cakephp.org/3.0/en/controllers/request-response.html#query-string-parameters
The solution
Hi, I did a little api to get the data I need from a json array.
So I created an ApiController to do all the ajax requests I need. Here is test example:
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\ORM\TableRegistry;
class ApiController extends AppController
{
public function test($id)
{
$UsersTable = TableRegistry::get('Users');
$users = $UsersTable->find()->where(['id <' => $id]);
$this->set(compact('users'));
}
}
?>
I want to get the list of all my users from an ajax call (that's the worst idea ever but it's just for the test ;))
Now I need to enable json for this view. For this, I need to go in config/routes.php to add this:
Router::scope('/api', function($routes){
$routes->extensions(['json']);
$routes->connect('/test/*', ['controller' => 'Api', 'action' => 'test', '_ext' => 'json']);
});
Now when I go on http://localhost/mywebsite/api/test/100, I have a json array of all the users with an id < 100, ready to get pulled by an ajax script.
'_ext' => 'json'
means you don't have to go on api/test.json to display the json array
Now on a random view in a random controller, I put a button to try all this and I call my javascript file to do my ajax request.
<button class="test">Test button</button>
<?= $this->Html->script('test', ['block' => true]) ?>
Now in my test.js:
$(document).ready(function(){
var value = 100;
$('.test').click(function(){
$.get('/api/test/' + value, function(data){
console.log(data);
});
});
});
I click the button and the log shows an array with all the users I wanted in my console when I push F12 (on chrome).
Hope this will help someone
When json response sends response.success == false, I can see the console log showing me the error, but x-editable seems that doesn't catch the return, and the value in the screen is changed to the new one I had introduced, although it has not been really saved. Is there something wrong?
Here is piece of the CGridView code I use:
'class' => 'editable.EditableColumn',
'editable' => array(
'model' => $model,
'params' => array('YII_CSRF_TOKEN' => Yii::app()->request->csrfToken),
'url' => $this->createUrl('user/update'),
'success' => 'js: function(response, newValue) {
if(!response.success)
console.log(response.msg);
return response.msg;
}',
'options' => array(
'ajaxOptions' => array('dataType' => 'json')
),
)
EDIT 1:
Ok, I have been working on that, and I have found which is the problem. It seems that the javascript function I put on success is not working properly.
The if statement is catching correctly the response, but the return value is not being sended correctly. I explain: if I put a literal like that: return "test return"; the value is returned correctly, but if I put return response.msg; nothing is sended.
Of course, response.msg is not empty and contains the String message correctly.
Ok, I have been working on that and I found my stupid mistake... I was returning msg as array and I had to do this:
return response.msg[index];
Where index is where the message is stored.
It was really embarrassing losing time with that...