As stated, when I use in a shell :
$this->requestAction('/sites/zaz/option1');
The action doesn't get triggered. To test this, I tried :
public function zaz($option1 = null) {
CakeLog::write('acces', 'action triggered');
return 'got it !';
}
And the action isn't done and there is no log written whatsoever. All my other logs do work.
So I tried :
$this->requestAction('/sites/actionwhichdoesntexist/option1');
And I got an error stating that the action doesn't exist.
I really need to use requestAction, because I have a model/controller and this action is typically testing that a ressource is still alive. I want to use requestAction in order to smoothly handle the "requested" aspect, so that I will build something more robust :
if(empty($this->request->params['requested']))
$this->redirect(array('controller'=>'proxies', 'action'=>'index', 'admin'=>true));
else
return true;
I tried with 2.4.1 and 2.5, nothing just happens, no output is given, even if I put a 'die()' in the action.
As stated in the first comment, I should have checked the beforeFilter function.
Related
I'm setting up a simple Ajax call in one of my forms. When a user enters characters in a field, the following Ajax call is activated:
self.modify = function (input_field) {
if ($(input_field).val().length > 5) {
$.post("{{path('get_bio_control_sample')}}", {sample_number: $(input_field).val()},
function (response) {
if (response.code == 100 && response.success) {
alert(response.sample_number);
}
}, "json");
}
};
Which is meant to access the following controller action:
class BioControlController extends Controller {
/**
* #Route("/bio_control/sample", name="get_bio_control_sample")
*/
public function getBioControlSampleAction(Request $request){
$sample_number = $request->query->get('sample_number');
$response = array("code" => 100, "success" => true, "sample_number" => $sample_number, "sample_data" => "test");
return new JsonResponse($response);
}
}
However, when the call is activated JS returns the error:
http://127.0.0.1:8000/omics_experiment/%7B%7Bpath('get_bio_control_sample')%7D%7D 404 (Not Found)
I'm accessing the Ajax call from omics_experiment/new (which is in the OmicsExperimentController) and using the route /bio_control/sample (as shown by the annotation), but it's not working. Can someone explain what I'm doing wrong?
I used this question as a template, the fact I'm using Symfony 3 might mean there are syntactic errors.
I just had to do this recently. I'm no expert on Symfony either, but since I just did this I may be able to help. Using Symfony is not really much different than doing it with a static URL. The main thing is to make sure that your controller and route are set up properly and working without AJAX, then you just need to use the path set in your route for the .post call.
And what makes it worse, is that it's really hard to test this type of interaction. Even your twig includes can cause it to fail if they are set up wrong.
Looking at your code again I think this may be the problem. Change this
$.post("{{path('get_bio_control_sample')}}", {sample_number:
to this
$.post("/bio_control/sample", {sample_number:
Because I think the way you have it is only good for twig templates, so if Symfony is not looking at your JQuery file like it does a twig template, then, it's not going to understand how to get the route.
Have a question on Slim Framework php.
In my application, I would like to stop the application execution if a condition is mismatched.
There is a halt function, per Slim documentation. But that does not appear to be working.
the application continuous to execute even after calling Halt.
pseudo code:
if ( $valid ) {
// Do something
} else {
$app->halt(500, "not valid");
}
// Other code here.
$app->run();
I was expecting that, we we call Halt function, the "Other code" should not execute.
But it does not appear to be the case.
Any ideas?
Halt should work. As Josh mentioned, it does need to be called from within a route callback. For example, I use the following as a custom 404 (inside of index.php) in the event that a specified route is not found.
// not found (custom 404)
$app->notFound(function () use ($app) {
// build response
$response = array(
'type' => 'not_found',
'message' => 'The requested resource does not exist.'
);
// output response
$app->halt(404, json_encode($response));
});
Note the reference to use ($app) -- more on this can be found here: http://www.slimframework.com/documentation/stable#scope-resolution
Halt should only be invoked within the context of a route callback. I recommend you ask any further questions on the official Slim Framework support forum:
http://help.slimframework.com
Best regards,
Josh
You can always call exit manually to stop the script execution.
$app->halt(500, "not valid");
exit;
You can use return if you are in a callback function
return $app->halt(500,"message");
or
$app->stop();
after halt function.
In PHP/Kohana, I have controller action method which does some processing. When it is finished, I want to send it to another controller, e.g.:
public function action_import_csv()
{
Kohana_Import_Driver_Csv::process_files_from_csv_to_mysql($this->import_directory);
//url::redirect(Route::get('backend_application')->uri()); //undefined method URL::redirect()
//redirect(Route::get('backend_application')->uri(), null); //undefined function
}
According to this documentation at least the first redirect should work. I'm using Kohana 3.
How can I send execution from this controller action method to a new controller/action?
Addendum
For some reason, url::redirect is not available, here is the code completion I get for url:::
#bharath, I tried url::current() and got this error:
The problem is that you are looking at the Kohana 2 docs. Go to the kohana homepage and find the correct docs. Also, for some reason, everyone is giving you Kohana 2 answers even though you stated you're working with 3.
To redirect, do this from the context of a controller:
$this->request->redirect($something);
$something could be:
controller
controller/action
http://url.com
Here are the api docs for the redirect method (note that this uses url::site to parse the url; you may want to look at the source of that method too.
i am not very sure but i think you can simple use the redirect() function passing in the other controller you want to send to with any parameters
example
redirect(controllername/method)
Shouldn't that be :
url::redirect('controller/method');
And if it doesn't work, you probably had some output before calling the redirect (you'll probably get the "Headers already sent" error when that is the case).
im trying to debug a symfony app.
I've added a debug_backtrace() calling to this function below. It
outputs a list of functions called, but the save() function (that is
just before the debug_backtrace() calling) is not that list.. why? any other way to debug that shows more things, in this case the save() calling ?
protected function processForm(sfWebRequest $request, sfForm $form)
{
$form->bind($request->getParameter($form->getName()));
if ($form->isValid())
{
$sf_guard_user = $form->save();
var_dump(debug_backtrace());
die("fsdgsgsdf");
$this->redirect('guardausuario/edit?id='.$sf_guard_user-
>getId());
}
}
Regards
Javi
I've just got my target using
xdebug_start_trace('/tmp/foo');
$usuario = $form->save();
xdebug_stop_trace();
http://www.xdebug.org/docs/all_functions
Javi
Symfony's web developer bar has some great information.
What exactly are you trying to see? Somethings it is good to echo $form because it will reveal all of the fields and any hidden fields in the form. Also, remember to include [_csrf_token] in your View if you are writing a custom View.
And... Symfony and xDebug are a good combination.
In php.net (http://php.net/manual/es/function.debug-print-backtrace.php) bishop exposes this solution:
$e = new Exception();
$e->getTraceAsString();
It gives a short but concise response for all the functions being called. It perfectly suited my needs, so I can log what happened without being lost.
I'm starting to write unit tests using the CakePHP framework and SimpleTest. The documentation describes a problem with the testAction method when your controller redirects the browser to another page. There is a hopeful note with a link to a possible fix, but the link is broken.
Has anybody gotten this working? Know how to find where that broken link should point?
I found a discussion of using partial mock objects to override the redirect call, but that doesn't seem to work with the testAction method. I suspect I'd have to somehow register the mock controller with the dispatcher.
Here's a similar question on Google groups.
I got something to work, so I thought I'd post it here. I'm not sure if I'm happy with it yet.
If you want to be able to test a redirect, change this:
$this->redirect(array('action'=>'index'));
to this:
$this->redirect(array('action'=>'index'), null, false);
return 'redirected to index';
Now your test can look something like this:
$data = array(...);
$result = $this->testAction(
'/people/edit/1',
array('method' => 'post', 'data' => $data));
$this->assertEqual(
'redirected to index',
$result);
The problem with the controller's redirect method is that it calls exit() by default, and that exits out of the entire test suite. This version passes false to the redirect method's $exit parameter, and then uses return instead of exit(). The return value is just something for the test case to validate if you like, although it is echoed to the browser along with the redirect header. As long as it's a small message, though, I don't see any problem with that.
There doesn't seem to be any significant code that might execute after the controller method when we call return instead of exit(). A quick test shows that the page behaves normally.