I am new to cakePHP.t I thought this should be simple but I am having hard time to find the reason why saving in a controller action does fail.
I tried to print error
if ($this->MembershipRequest->save($this->data['MembershipRequest'])){
$this->flash("All items are saved", array("controller"=>"home", "action"=>"index"));
} else {
pr($this->MembershipRequest->validationErrors);
}
But it does not print anything. Could anybody give me right direction?
I am looking for a good way of debugging cakephp app. One thing I just found out is Configure::write('debug',...) which gives great debugging messages. Is there any other tool? Thanks
I'm not sure of your format of $this->data, but I'm pretty sure you don't need to pass the "MembershipRequest' as a key in the save, try saving with $this->MembershipRequest->save($this->data) and it should work.
Also, check the beforeSave() methods you have in your MembershipRequest or AppModel, they should always return true, otherwise the save will silently fail.
For other debugging help, I suggest you look at the CakePHP DebugKit.
Related
I have a very special problem and I don't know how to deal with it.
I have web App in Laravel, when i open index page, I receive text message to my mobile phone.
Problem is, sometimes I receive 2 messages or 3, sometimes 1.
Is there a tool how to debug this strange behavior which is not always the same?
A few words about my code:
user opens the page, and because its first visit Session doesn't have attribute message_sent and SendTextMessage::SendMessage($phoneNumber, $id_message, $smsCode, $newDateFormat); is executed. After that Session has message_sent and can't be sent again, for example if I refresh the page.
SendTextMessage::SendMessage() is Class in Laravel Helpers.
controller code:
public function index($url_attribute, $id_message, Request $request)
{
if(!Session::has('message_sent'))
{
$user = User::where('id_message', $id_message)->first()->toArray();
$phoneNumber = $user['mobile_phone'];
$smsCode = $user['sms_code'];
$newDateFormat = date("d.m.yy", strtotime($smsExpirationTime));
$request->session()->flash('message', 'Text message sended.' );
SendTextMessage::SendMessage($phoneNumber,$id_message, $smsCode, $newDateFormat);
Session::put('message_sent', true);
}
return view('login');
}
SendTextMessage Class:
class SendTextMessage
{
public static function SendMessage($phoneNumber, $id_message, $smsCode, $newDateFormat)
{
$sms = new Connect();
$sms->Create("user","pass",Connect::AUTH_PLAIN);
$sms->Send_SMS($phoneNumber,"Message");
$sms->Logout();
}
}
Many thanks for any tip or help.
UPDATE:
problem is only in Chrome.
Edge and internet explorer are fine.
As this script runs on server-side the browser shouldn't be an issue. Based on your code provided, there is no clear answer to give here.
Please try the following in order to debug your problem:
Log messages at each stage of the script in order to see which part was called how often. That will help you to locate the problem. You can use \Log::error("Message") to do that.
Once you know where the problem might be, try to log "decision" making / mission critical variables to logile as well. E.g. \Log::error($session) so that you can understand why that problem might occur. One reason could be that you have a bad configured session caching or your cookies might be messed up. At some point there is probably a piece of data not the way you expect it to be.
You should maybe try to change the way you use Laravel Session.
You indicated that it was working fine on some browsers, that means your server-side code is correct so far, but there is someting messing with Chrome… From there,
if you take a quick look at the Laravel Session doc, you'll see that Session can be stored in cookies, and I bet that this is your actual setup (check in your .env file the SESSION_DRIVER constant, or in your config/session.php file).
If so, to confirm that this cookies-based session setting is the culprit, you might want to change the Session config to make it browser-independent: any other option than cookies will work, the database or file options might be the easier to setup… And if it works I would strongly encourage you to keep using this no-cookie setting to make your code browser-safe.
Good day,
I have been struggling with this for the past few hours but admittedly I'm not very familiar with Laravel.
I use this row of code to generate a cookie:
return Response::make(view("/play"))->withCookie(cookie('username', $request->input('username'), 60000));
I have logged $request->input('username') and it is not empty,
the application goes to the /play view but when I use Cookie::get('username')in a later method in another Controller later on it returns null.
picture of cookie storage it also does not appear empty in the browser.
Does anyone know why this is happening and how to fix it?
try Request()->cookie('username')
My question might be little silly but please bear with me. I suppose var_dump should work anywhere in the code which calls its service but unfortunately i can't return anything if i use it in a controller, or model. ya it does work in the view/layout page.
I tried testing the following simple thing in one of my controller function and it returns nothing;
$foo = "bar";
var_dump($foo);
Please enlighten me!
I don't know Joomla, but in an MVC framework, the view expects data to come from the controller in a particular format, perhaps JSON or XML. When you call var_dump(), it will most likely mess up the syntax of this, so the application won't work. When you're debugging with this tool, you'll want to make use of the browser's console (Developer Tools or Firebug) to view what was returned. Go into the Network tab, select the URL of the controller, and then view the response data. There you'll see the output of var_dump().
For show a variable and pause the executing you have 3 options.
echo $variable;
print_r($variable);
var_dump($variable);
and you need to write die() after them to stop code and show your $variable.
I started learning this very nice PHP ORM api last week: http://phpdatamapper.com/
and have been trying to get up to speed with it
What I'm not seeing in the site documentation, is how to iterate with "$postMapper->all()"?
http://phpdatamapper.com/documentation/usage/finders/
When I try to iterate through the value returned from ->all(), it seems only to have gotten the last element in the table.
Here's the code I have:
// $postMapper uses phpDataMapper framework. It works to create the schema & insert values
$postEntities = $postMapper->all();
$postEntities->execute(); // tried adding this to help things
foreach ( $postEntities as $postEntity);
{
echo $postEntity->title;
echo "<br/>";
}
I see other folks are forking it from GitHub and using it in their projects, so I believe I'm making some mistake in my call logic.
It would be great if someone could share a small example of how to access the query data correctly when using mapper->all()? This is an important part of a PHP stack and I would very much like to be able to use this particular solution in my projects going forward. Thanks
Ok, this turned out to be a "not so smart" mistake on my part. The font in my IDE was too small I guess, and I didn't see the ';' at the end of the line that declares the for loop.
So the ORM is all good, just my code wasn't. I'm updating this in case someone else can learn from my mistake - other option is to just delete the post all together (... not sure which is better)
When I display an error message in php, I usually do it like this,
if($query){
// Success code
}else{
// Error message
$error_msg = "An error occurred.";
return false;
}
and I echo $error_msg variable in the front-end html page.
I am just wondering if that return false after the variable actually does anything, or is it just pointless to put it there?
I am trying to optimize my code and just wanted to make sure! Thanks a lot in advance!
Yes, it tells the function to stop executing, and to return a value "false". So it does something. Whether or not is does something USEFUL depends on your programming.
If you have no executable lines after the echo, and the function is not required to return a value, then it won't actually do anything useful.
If does make it clearer to readers that "this is a stop/failure point" though. And one day you might want to actually trap if it worked or failed - so leaving it in makes it clearer. You also may extend the function without thinking and need to retro-fit the returns - again, leaving it in makes it easier.
On the converse, there is also the old programming style from C / Assembler days that you only have one entry and one exit point for all functions to help with garbage collection. Not a requirement with PHP, but that style does make it nice and neat. In this case, set the return value (if required) and return at the end.
So, go with which suits your style - but think ahead. Making everything pristine and minamlistic (i.e. dropping the line as you don't strictly need it) may not always be the best approach.
The point of the return false is so that you can easily test a function to see if it has worked - without needing to go into any potential error messages and the like.
A function that returns false for a fail can be used in the following manner:
if(someFunction($var))
{
do stuff for function working...
}
else
{
// function failed. Do other stuff.
}
It is also intuitive that a function returns false when it fails. It can be further used like this:
$foo=someFunction($var);
if($foo)
{
again, function has worked.
}//... etc
While the same function returning The function has failed could not be used in tis intuitive manner.