redirect to another function in a controller in magento - magento

I have magento site.
I have a controller ,how can i redirect into another function in this controller?
That means ,there have two functions:
1.test()
i want to redirect from test1() to test2()
how can i do this?
How can i redirect from one function to another function in the same controller?

There are three redirecting functions available in frontend controller which are:
_redirect()
_redirectUrl()
_redirectReferer()
_redirect('frontName/controllerName/actionName/param1/param2') is used for internal redirection.
_redirectUrl($fullUrl) is used for external redirection.
_redirectReferer() is used to redirect to the referer url.
Hope this gives more info.

You can put something like this in test1 action.
$this->_redirect('*/*/test2', array(if you want to pass something then from here you can pass ));

Related

how to use two functions from same controller in single page route using get in laravel

Am trying to use two different functions from one controller in a single page route
Route::get('/cart','App\Http\Controllers\Frontend\CartController#index');
Route::get('/cart','App\Http\Controllers\Frontend\CartController#alldata');
But the problem is the function alldata works where the function index doesn't
You can't have 2 GET routes with the same path.
Route::get('/cart','App\Http\Controllers\Frontend\CartController#index');
Route::get('/cart/all','App\Http\Controllers\Frontend\CartController#alldata');
The /cart route is overwritten by the alldata(). So the alldata() is calling instead of index().
kindly remove the alldata()'s route and pass the data from index().
Route::get('/cart','App\Http\Controllers\Frontend\CartController#index');
Route::get('/cart','App\Http\Controllers\Frontend\CartController#alldata');
Try to manipulate your logic in controller rather than in route file.
Use conditional in controller function.

(CI 4) How to redirect within Controller?

how can I refer to another within one controller? In CI3 (for example in the controller client.php) I solved this as follows:
redirect('/Clients', 'refresh');
but that no longer seems to work in CI4. (Msg: "route cannot be found while reverse-routing.")
I Also tried
redirect()->route('/Clients');
but the error is the same.
redirect()->to('/Clients');
redirects nowhere (no output, nothing)
For a better understanding: I want to use a controller (e.g. Clients/create to Clients/details)
What you should notice is that redirect() does not just set headers like it used to do in CI3. In CI4 it returns a RedirectResponse object with which you can ask your controller to do a redirection.
To do so, you need to return this RedirectResponse object inside your controller. Without the return statement, the redirection won't happen.
An other thing to notice is that redirect() can be called with some "options" :
Going to a named route
return redirect()->route('named_route');
or
return redirect('named_route');
To use this, you need to add a named routes in your app/Config/Routes.php file :
$routes->get('/', 'MyController::index', ['as' => 'named_route']);
Going to a specific URI
return redirect()->to('Clients');
It will redirect you to your base url with /Clients at the end.
Please check out the doc for further informations : https://codeigniter.com/user_guide/general/common_functions.html#redirect
#ViLar gives the correct answer, but it's important to note that when using auto routing the CI3 version redirect('home'); becomes return redirect()->to('home');
It's confusing to just say "Going to a specific URI" and omit that this means auto routed controllers.

Whats the difference between redirect and this in Codeigniter?

I am new in Codeigniter and it's one of the good frameworks of php. But on some conditions I'm confused. Like this one. If any of you have any clarification about my dough, it's a great help for me.
Offcouse redirects refresh the page and $this not but apart from this I want to know - anyhow both of them used to go to somewhere else on view pages or like in other controller or in same controller to other methods.
But we don't use these side by side because when getting any of them it will go to that page or method without checking the next lines.
In case of a normal difference then have lot's of but I just want to know about the condition of going to next page or method when we use redirect or $this like this -
$this->Function($value); //It's method of same controller.
redirect('Controller/function'); //It's also doing same with page reload.
Thank for looking my problem.
Redirect()
When you will call any function of helper in codeigniter then you can call function directly without using any object. Helper in Codeigniter is collection of functions.
Redirect() method is a part of URL helper in Codeigniter.
For your ref. https://www.codeigniter.com/user_guide/helpers/url_helper.html
So, just load helper using $this->load->helper('url'); or you can also mention in autoload.php file.
$this->Function(); used to call a function from same controller
$this->Function(); used to call a function from same controller
redirect()
While building a web application, we often need to redirect the user from one page to another page. CodeIgniter makes this job easy for us. The redirect() function is used for this purpose.
redirect($uri = '', $method = 'auto', $code = NULL)
The first argument can have two types of URI. We can pass full site URL or URI segments to the controller you want to direct.
The second optional parameter can have any of the three values from auto, location or refresh. The default is auto.
The third optional parameter is only available with location redirects and it allows you to send specific HTTP response code.
Redirect means jumping to another function mentioned in the redirect method.
$this->Function($value); => jumping to another function and you can execute the code of the same function as well as pass the value back by returning value.
When you send request to codeigniter generally CI controller gets called and then function which is mentioned in uri segment. like below... So this will be another request.
redirect('Controller/function'); //It's also doing same with page reload.
But when you have to call another function within the same request then you can use below approach
$this->Function($value); //It's method of same controller.
This will execute the given function and return the value within same request.

when i call a function in a controller, it show me 2 time the controller which stop my task

Hello every body I have a problem when i call to a certain function in controller to update a record, it duplicate the my controller in URL which not let me work fine.Help please thanks in advance..
it show URL like this:
localhost/codetest/practice_controller/practice_controller/practice_update/1
In above practice_controller is coming two time.
Two possibilities.. either you have a redirect code in your controller which is redirecting in a false way or in the form action you have not written complete URL starting with base_url().
I would suggest to write complete url in form action like
<?php echo base_url()?>practice_controller/practice_update/<?php echo $update_id?>

How do I redirect or call a different Controller Action from my Controller Action in Magento?

I want to call cms/index/noRoute Action from one of my custom module's controller action, How do I do it?
I tried,
$this->_redirectUrl('cms/index/noRoute')
and
$this->_forward('cms/index/noRoute')
also few variations for redirect URL like '*/cms/index/noRoute' etc, non of them worked. How should I do this?
Use below code :
$this->_redirect('defaultNoRoute');
return;
its work for me.
Found it, and its so simple,
$this->norouteAction();
Since I'm doing this in one of my controllers which is extended from Mage_Core_Controller_Front_Action which is again extends from Mage_Core_Controller_Varien_Action, gives me the ability to call norouteAction().
I think this should do the trick (untested): $this->_redirectUrl($this->getUrl('cms/index/noRoute'))

Resources