Codeigniter controller, javascript console.log() - php

Is there a (simple) way to echo some variables from a Codeigniter controller to the browser console?
Something like
public function my_controller_method() {
if (testing_out_something) {
$result = "looks like it works";
$this->log_to_javascript_console->write($result);
}
}
I saw this javascript class but it looks like it's more for jQuery, I just want to do something simple for debugging purposes.
I guess another way would be to send it to a view, but I don't want my view to change at the moment, I just want to make sure my data is being sent.

this links may help you :
http://www.codeforest.net/debugging-php-in-browsers-javascript-console
http://sarfraznawaz.wordpress.com/2012/01/05/outputting-php-to-browser-console/

Related

PHP How to make a function accessible with and without the call in the same file?

Sorry if my question is not clearly understandable, I don't know how to express well what I want to say in English.
This is not a problem with code per se, as it is working as shown, but mostly a doubt of the behaviour of PHP.
When I call a function from another php file, it seems to read the function itself i.e. function loademp(){}
however, if I access the file containing the function from an ajax, it seems to need a call to the function i.e loademp() to be in the same file.
Since I had this issue I ended having this code in order to make it work from both origins, with the call for the ajax inside an if condition, otherwise it would be called twice from the php file:
<?php
if ($_POST['runFunct']=="loademp"){ //call from ajax needs 'loademp()' to access the function;
loademp();
}
function loademp(){ //loaded from another file apparently.
try{
//PDO code
print_r(json_encode($results));
}catch(PDOException $e){
echo $e;
}
}
My other file just look like this:
require __DIR__.'loademp.php';
loademp();
Isn't there a more practical way to just use the code for both cases with no conditioning depending on the origin? Since I can't call a specific function from ajax without the use of POST variables, I guess this is the best case for it, but I would appreciate if you could point out the good practices about it.
I think your confusion here is between defining a function and executing a function.
To define a function, you write something like this:
function say_hello_world() {
echo "Hello, World!\n";
}
This doesn't cause anything to happen immediately, it just defines how to do something. In this case, it's basically like saying:
Whenever I ask you to "say hello world", output to the screen "Hello, World!\n"
To make something actually happen, you have to execute the function, which looks like this:
say_hello_world();
That's basically saying:
Do the actions I gave you for "say hello world"
In your example, your file 'loademp.php' defines a function called loademp - it says "whenever I ask you to 'loademp', here's what I want you to do". In your other file, you include that file, so the function is defined. Then, you run it with this line:
loademp();
An AJAX call is no different from any other page load, so you need to do the same thing there - first, define the function, or include the file that does; then, execute the function.
So, rather than calling loademp.php directly, you could call a PHP script like define_and_execute_loademp.php with exactly the lines you've mentioned:
require __DIR__.'loademp.php';
loademp();

Hand over "data/params" on reroute(); in "fat free framework"

Im looking for an elegant way to hand over data/params when using $f3->reroute();
I have multiple routes configured in a routes.ini:
GET #sso: /sso/first [sync] = Controller\Ccp\Sso->first, 0
GET #map: /map [sync] = Controller\MapController->second, 3600
Now I reroute(); to #map route, from first();
class Sso {
public function first($f3){
$msg = 'My message!';
if( !empty($msg) ){
$f3->reroute('#map');
}
}
}
Is there any "elegant" way to pass data (e.g. $msg) right into $MapController->second(); ?
I donĀ“t want to use $SESSION or the global $f->set('msg', $msg); for this.
This isn't an issue specific to fat-free-framework, but web in general. When you reroute, you tell the browser to redirect the user's browser page using a 303 header redirect code.
Take a minute to read the doc regarding re-routing: http://fatfreeframework.com/routing-engine#rerouting
There seems to be some contradicting information in your question, which leads me to question the purpose of what you are trying to achieve.
If you are rerouting, you can either use the session, cookies, or use part of the url to pass messages or references to a message.
If you do not need to redirect, but just want to call the function without changing the passed parameters, you could abstract the content of the function and call that function from both routes. You could also use the $f3 globals, which are a great way of passing data between functions in cases where you don't want to pass the data using the function call. is there a reason why you don't want to to use this? The data is global for the single session, so there is no security concern, and the data gets wiped at the end of the request, so there is very little extra footprint or effect on the server.
If you're alright with not using #map_name in re-routes you can do something like this:
$f3->reroute('path/?foo=bar');
Not the prettiest I'll admit. I wish $f3->reroute('#path_name?foo=bar') would work.

Creating a sub-page in CodeIgniter using URL routing

I have a form which is starting to get a bit too complex, so I want to split one page into three different pages. I understand that I need to use URL routing to achieve this, but no luck so far. Reading the manual didn't help.
Current URL is as following:
index.php/profiles/edit/$id
I want to achieve something like this:
index.php/profiles/edit/contact/$id
index.php/profiles/edit/pictures/$id
...
You do not need to do routing. You can simply improve your edit function like this.
public function edit($type,$id)
{
if($type=='contact')
{
//do contact task
}
if($type=='pictures')
{
//do picture task
}
}
Now your expected links will work.

How to use var_dump in joomla

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.

How to get all the variables available in a view in PHP?

I need to see all the variables that are available in a view. I am a front end developer so I mostly work in the views directory. I don't always know which variables are being passed to the templates by the back end dev. Instead of asking him every time an easy solution would be some type of snippet that I can temporarily paste into the view that I'm working on so I can see all the available variables and even better if I can also see their types and values.
I tried this:
<pre><?php var_dump(get_defined_vars()); ?></pre>
But since I am using Codeigniter it also shows all the other tons and tons of variables that are passed in by the framework.
I only want to display the variables that were passed specifically from the controller that loaded the view. Is there any way to do this?
var_dump($this->_ci_cached_vars);
One possibility could be to do something like this:
$data['user'] = $user;
$data['cart'] = $cart;
$data['data'] = $data;
$this->load->view('view', $data);
If you did something like this, then you could always access a data array that looked the same as before it was parsed for the view.
Then you could use something like print_r or whatever you wanted to take a look at the array.

Categories