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).
Related
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.
I am always getting the this error.
{"status":false,"error":"Unknown method."}
But all syntax are correct from my side. because everything is working fine on the browser but same URL integration on the devices gives the 'unknown method error'.
I am using this 'get' method. Sample URL
SITEURL/api/login/test?req_type=custom
Am I missing something while integrating? Perhaps a setting? I have just included the library and rest config file.
I think your problem is the name of controller is the same with the name of method try to make a test:
if the name of your controller is:
class Test extends REST_Controller{
//your method name is different from the name of controller class
public function testget_get(){
echo $this->response(array('test'=> 'test'), 200);
}
}
I have experienced this problem on hmvc structure.
You also need to check that from device which method your are getting means they are sending 'POST' or 'GET' so you can update your function name accordingly.
In my case I have done the function name as _get to the methods but from device methods of sending parameter is 'POST' which I am trying to access as 'GET'.
So please cross check this once.
When you create a method with the library, you need to append the type of request you are going to make to it.
So, if your method is test, and you are making a GET request to it, it needs to look like this:
function test_get(){
...
}
Same with POST requests
function test_post(){
...
}
Same with PUT, and DELETE as well.
NB This is only a guess since you didn't include any of your code for some reason.
Im currently building a controller from my Zend MVC application which would only be used as json service to populate the page. I want to restrict the users to use only GET method to access this end point(for some security reasons).
I followed this post _forward() in Zend does not work? but could not get working.
I am using the preDispatch to detect the non-get requests and would like to forward to errorAction in the same controller. My code looks likes this,
public function preDispatch(){
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
//Restrict this Controller access to Http GET method
if(!($this->getRequest()->isGet())){
return $this->_forward('error');
}
}
public function errorAction(){
$this->getResponse()->setHttpResponseCode(501);
echo "Requested Method is not Implemented";
}
When I test the page with a post request, it throws
PHP Fatal error: Maximum execution time of 30 seconds exceeded
I got it working with
$this->_redirect("service/error");
Wondering if it is the only/best way to handle this situation.
Any help would be really appreciated. Thanks in advance.
The reason that calling _forward doesn't work is because the request method doesn't change so you end up in an infinite loop trying to forward to the error action since the request is always POST.
_forward works by modifying the module, controller, and action that will be called when the request is dispatched, _redirect actually returns a 302 redirect and results in an additional HTTP request by the browser.
Either method is okay, but I'd prefer to go with _forward since it won't require an additional HTTP request (but you still guarantee the POST request is denied).
This code should work for you:
if(!($this->getRequest()->isGet())){
// change the request method - this only changes internally
$_SERVER['REQUEST_METHOD'] = 'GET';
// forward the request to the error action - preDispatch is called again
$this->_forward('error');
// This is an alternate to using _forward, but is virtually the same
// You still need to override $_SERVER['REQUEST_METHOD'] to do this
$this->getRequest()
->setActionName('error')
->setDispatched(false);
}
I am learning how to use codeIgniter as my php framework. I am reading through the documentation and watching the intro video and just generally following along with the first tutorial, but it's not working for me.
I have created a controller called "test.php" and a view called "test_view". The controller for this class is exactly like "welcome.php" and the view file just has some static html. However, when I go to index.php/test I get a 404 error.
I have also tried manipulating the original welcome files so that instead of calling a view it just echos "testing", yet I still see the original welcome message! I've tried clearing my browsing cash and refreshing, but to no avail.
Any suggestions? Thanks.
Edit: Here's the code for controllers/test.php
<?php
class Test extends Controller {
//Just trying to get it to echo test
public function index()
{
echo "test";
//$this->load->view('test_view');
}
}
?>
Try looking at this page in the documentation - this might solve your problem.
This basically means you should try typing index.php?/test/ instead (notice the question-mark).
First of all, check the above link. Might be useful.
If not, then...
Try changing the default controller in the config file ('routes.php') to something else (probably, to 'test'), then try loading index.php. Just to test whether the whole system works (or not).
Check whether mod_rewrite is loaded (in your server .conf-file, if you're using Apache).
Try using the latest build of the framework. AFAIK, the name of the controller class for now is "CI_Controller".
Finally, try removing the word 'public' before the declaration of the function. AFAIR, CI enable you to make private functions in controllers just by using prefix (which is set in the config file) at the beginning of the name of the function (thus making all the other functions public).
But most certainly the problem is with the mod_rewrite. If not, try debugging with die('Page found'); instead of echo - this will allow you to track possible redirects on the page.
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.