From the http context class is there a method to get the current Controller name?
Yes, you can do something like that
HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString();
If you're in a view, then you can do:
ViewContext.RouteData.Values["Controller"]
Related
I need to call a controller say 'faq_view' inside admin controller as the URL structure admin/faq_view like this how I can do this?
e.g:
site.com/maincontroller/function
site.com/maincontroller/othercontroller/function
Then, just redirect the page. Else if you want to just call the function, call it via AJAX.
It depends what you exactly want to do. If you want to just invoke the function, its not the right way. Controller as it defines itself controls the flow of the pages that comes on sequence. Controller is responsible to send commands to its associated view to change the view's presentation of the model.
So, if you are saying you want to call controller within another controller, that should mean you are about to redirect to another page.
Updated answer:
Just assume you have new_function on maincontroller that calls the function from othercontroller. The function does not need to be defined on othercontroller.
Add the following line on routes.php.
$routes['maincontroller/new_function'] = 'othercontroller/new_function';
Now, you can call the function of othercontroller as maincontroller/new_function.
You can always call a controller inside another controller, but this only works for calling one controller as far as I have tried. Let's say you are trying to load a controller inside a controller. You can try this:
$this->load->library('../controllers/myothercontroller');
Then do this:
$this->myothercontroller->function_name();
That's it! You can now access any function inside myothercontroller (controller) in your current controller. I hope this helps too.
Your controllers are part of the presentation layer and should not contain application logic. That means you should never need to call a controller from another controller, instead refactor your application and move the domain logic to the model layer.
Now if you have a method that you need in multiple controllers, say for example you need a template method that automatically adds your header and footer views.
If that is the case, create a base class that your controllers extend.
If you are talking about just a routing issue, then just use the routes file for that. I don't like the CI automatic routing and it should be avoided as it will result in duplicate URLs for the same resource.
I have a Locations controller and Location model
I want to use the the Location model in another controller but calling
geddy.models.Locations.all(...);
does not work. It says Locations is undefined.
How do I make it load the model manually? Or should I create a function in the Locations controller to grab the data using a request on the client or a call directly to the controller on the sever?
Looks like you're pluralizing Locations when it should be singular: Location - hope this helps.
I have a controller where most every method should be restricted to Role=Admin so the class instantiates with:
[Authorize(Roles = Admin)]
Every method takes on this check. I know I can override this attribute method by method so as to permit other user roles but I'd like to remove the Authorization check completely for one of the methods in this class.
What's the syntax for that?
thx
Wasn't able to implement Peter's suggested link so I removed the global attribute and added it back to each of the controller's methods except the one I wanted.
I'm writing my own helper and it needs to get a path as a parameter. I can of course refer to it directly passing a string or using Url.Action(). But what if I want to generate fully qualified URI inside of my helper method using action, controller and route values?
It does look a little bit messy right now
#Ajax.MyHelper(Url.Content("~/Admin/Administration/DeleteItem?Id=<#= Id #>"))
Inside the helper, you can call
new UrlHelper(ajax.ViewContext.RequestContext).Content(...)
I want to call a method before the execution of every and each controller's method. I don't want to go and call the method in every method. I just want to call it from one place and it will be called before any method of any controller in magento.
And I am sure we can do this but I don't know how it can be accomplished.
Please provide your suggestions.
Hope we can resolve this or may some expert guys already resolved this.
Thanks.
You need to create an Observer that binds to the controller_action_predispatch Event. That will fire before every controller in the Magento codebase. There's a useful wiki page here that walks you through the process.
You have to create a method called preDispatch in your controller. This method is executed before the requested controller action.
something like:
public function preDispatch()
{
parent::preDispatch();
//my code here
}