How do I redirect or call a different Controller Action from my Controller Action in Magento? - 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'))

Related

Codeigniter skip login check, define in constructor, for specific action

I defined a login check in my controller constructor method like following code:
if($this->session->userdata('log_in')== false){
redirect(); //function for redirect to login page
}
But I dont want to call this login check for a specific action
(say public function viewProduct())
of the same controller. How can I do that?
You can specify your method name in constructor in if condition like
$method = $this->router->fetch_method();
if($this->session->userdata('log_in')== false && $method !== 'viewProduct'){
redirect(); //function for redirect to login page
}
Now it will be not checked in viewProduct method.
You cannot. You can either run the login check within the individual controller methods or put the view product into it's own controller that does not do the login check in the constructor.
Thinking about this, I suppose you could check the segments within the constructor like this:
if ( ($this->session->userdata('log_in')== false) AND ($this->uri->segment(2, '') != 'view_product') )
redirect(); //function for redirect to login page
}
The uri_segment is explained here: http://www.codeigniter.com/user_guide/general/urls.html#uri-segments
Not sure this is a great idea though. I would do one of the first two suggestions myself. But that is just a matter of preference.
Hope that helps,
Paul.
I think the best way is to pass through method in controller rather do it on constructor. Constructor first run while loading the controller so handling through that is not a good practice. But certain hooks can work for you but you should really follow the standard practice for future coding.

Codeigniter - custom routes

Lets say I have a controller 'standard' with an action 'main'.
If I go to www.myapp.com/standard/main it will invoke the main action in the standard controller.
Now if I want to change the URL I go into the routes.php and set sth like the following:
$route['welcome'] = "standard/main";
Now I can visit www.myapp.com/welcome and it will invoke the same action.
However it is still possible to visit www.myapp.com/standard/main. I now have two routes which lead to the same controller action.
Is this the usual way or should I somehow disable the now unwanted www.myapp.com/standard/main route? If so, how would I do that?
Thanks in advance.
That is completely for you to decide the default behavior. If you decide to disable the original url, you can write something like
$route['standard/main'] = 'standard/404';
Or any custom location, it depends on your project and how you wish it should look like, for example you can 'block' any method from being accessed directly:
$route['standard/(:any)'] = 'standard/404';
But bear in mind, if you route like you did $route['welcome'] = "standard/main";, then you should always remember that the function $this->uri->segment(n) will show different values, for example if I will go to url yoursite.com/welcome/123, the value of $this->uri->segment(2) will be 123, but if I visit the original URL yoursite.com/standard/main/123, it's value will change to main.
Bear that in mind and decide for yourself!
Assuming that this is a common structure you'll be using, for each action, you can check the segment's in the URL and then redirect appropriately. In the case of going from standard/main to welcome, you would have the following:
class Standard extends CI_Controller {
public function main() {
if($this->uri->segment(1) == 'standard') {
redirect('welcome', 'location', '301');
}
}
}
This would then check the first segment in the URL and if it sees that you've come to this URL, redirect you to the appropriate route. This can then be applied to each action you want to do the same thing for. Supplying a 301 signifies a permanent redirect.
Update
Just found a similar (the same?) question: Only allow URL's specified in routes to be seen in Codeigniter

redirect to another function in a controller in 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 ));

Using and hiding default class

This is my first time getting my hands dirty with CI so I'm getting a little confused.
I'm wanting to accomplish a couple things with my question. First of all, I'd like to always use the default controller without having it to appear in the url. For example, I created a new class named after my site (Example.php) and that works fine. However, if I want to call the search function in my controller I then have to go to example.com/index.php/example/search/.
The second thing I want to accomplish is when I run a search I'll get a nice looking url like so: example.com/search/This+is+a+search (I haven't gotten to removing the index.php portion but I know to use a htaccess). I'm not worried about the actual mechanics of the search, just that I'd like to format the url in this way.
I originally experimented with using a Search class but that found that it doesn't allow me put the search in the url because the second parameter should be a function and not the extra stuff.
Thanks for any help.
In application/config/routes.php file add $route to redirect everything to your controller.
Something like this:
$route['([^\/]+)'] = 'content/index/$1';
$route['([^\/]+)\/([^\/]+)'] = 'content/index/$1/$2';
This will redirect urls like example.com/A and example.com/A/B to a controller named content. Parameters A and B will be passed to method index.

Redirect CI problem

I'm kind of new with CodeIgniter and I'm still learning (a lot).
So I have a view and when I submit a form I 'call' the controller by surfing to the right URL dynamically e.g. site/delete
class Site extends Controller {
function index(){$this->load->view('...')}
function delete() {
$this->site_model->delete_row();
$this->index();
}
}
Now when that action is done (deleted the row) I'm calling $this->index(); to redirect to my initial page (which is good) but my url stays: site/delete . I want my URL to be ../site/index (or without the /index)
Any help would be appreciated :-) .
So far I found something to solve this:
instead of:
$this->index();
I'm using:
redirect('site');
Does anyone know this is a good practice?
Redirect is what you should use.
In the user guide:
http://codeigniter.com/user_guide/helpers/url_helper.html
they use it after checking if a user is logged in. Depending on if they are or not, they redirect to a different place.
Also, note that any code after the redirect won't run. Make sure and redirect after you've done everything you need to.
My preferred method is to have actions like that handled by the same method that will be seen by the user afterwards.
What if you go to /site/delete afterwards, as a user? It will either have to detect and throw a error (show a message) or redirect to an appropriate page. /site/delete has no meaning.
For example, if a user would normally see an overview after deleting, then my form will be posted to /site/index; with index quickly checking for the condition and calling _delete() in the same controller, before doing its normal work.
That way, if the user refreshes the page, or presses 'back', things should look consistent to them.
Another example would be that /settings/edit would post to itself - this means that it can act on the post and show any output (e.g. validation errors). It means there's no /settings/do_edit location on my site, and also means that the user can go back to /settings/edit safely, and see a form for editing their settings.
I suppose this is a subjective take on a perhaps objective question, and I would encourage feedback on my view, but it's my way of avoiding the problem you have asked about.
$this->index();
Call of function in a function simply execute the functionality within that function.
And url never changed.
for changing the url you should use.
redirect ( base_url().'site');
but you should load url helper in constructor.

Resources