call custom controller in magento in java script - magento

I create a custom module named connector,company name is social.in that,there is a controller i.e. Social_Connectors_Customer_AccountController .in this controller ,action method is
public function connectAction()
{
$this->_redirect('customer/account');
}
now,when i call this action method on click of my link,controller is somehow not called.and give me error like :The page you requested was not found, and we have a fine guess why.
java script in my phtml file is:how to call controller's action method in this script?
document.observe('click', function(e){
var target = e.findElement('a[rel^=google]') || e.findElement('button[rel^=google]');
if (target && target.readAttribute('rel')=='google')
{
alert('<?php echo $this->getUrl(); ?>');
}
});
or can anybody say how to call controller on click of my link?

to call controller action,you must have to follow strucure of mvc magento module.and the function getUrl() must be define properly.check its defination.

Related

How to get controller action by passing URL in laravel

I searched more time to find how to get the controller method name by passing the URL but not found my expected answer. I want to make a method where I will pass a URL and it will give the corresponding controller action like as below but I can't figure out.
I found a helper which just return the current URL's action which is Route::currentRouteAction()
If a route in my application like as Route::get('/abc', 'YourController#method') which will generate the url http://example.com/abc
then how can I get the YourController#method by passing http://example.com/abc
function getAction($url){
//what will be logic?
// return like App\Controllers\MyController#method
}
I have to make a custom permission system where I need it for show and hide the menu by checking the URL of each menu.
Within your controller you can do the following:
<?php
use Illuminate\Routing\Router;
use Illuminate\Http\Request;
public function index(Request $request, Router $route)
{
$action = $router->getRoutes()->match($request)->getActionName();
// action should be what you're looking for.
}
You can try this if you want to:
Route::get('/the/url', 'YourController#method');
Every time anything calls the URL in the route, your method will be called.
You don't need to navigate to that url to call your method, it could be called by a form action, or a buttons action and just execute your method.
Edit:
url is your url as parameter (plain route)
import this:
use Illuminate\Routing\Route;
this is your function:
public function method(Route $route, $url)
{
$routes = \Route::getRoutes()->getRoutes();
foreach($routes as $r){
if($r->getUri() == $url){
$youraction= $r->getActionName();
dd($youraction);
}
else{
dd('does not exist');
}
}
}
Tested.

How to call a controller from another controller in codeigniter?

I want to load a controller function from another controller function using codeigniter. What is the suitable way to do this so when call it url should be changed also.
No You cant do it.
What you have to do it is create that function in model and call it through your controllers. So it will work fine.
Ex
In Model
function get_id()
{
//some argument
}
In controller 1
$this->Model_name->get_id()
In controller 2
$this->Model_name->get_id()
yes you can (for version 2)
load like this below inside your controller
$this->load->library('../controllers/whathever');
and call the following method:
$this->whathever->functioname();

Custom Error Pages for particular controllers in CodeIgniter

If we call a method abc() within a controller named Example. Suppose abc() is not present in Example controller.
In such cases i need to display a custom error message instead of
404 Page Not Found
The page you requested was not found.
for this particular controller only.
I know we can set custom error pages, but it applies to all controller.
I need to use it with one controller only.
For Eg:
class Example extends CI_Controller
{
function index()
{
echo "index page";
}
function xyz()
{
echo "xyz page";
}
}
if i call example/xyz it displays output as 'xyz page'
but if i call example/abc it show page not found error. (i need custom message for this controller only).
Thank You...
You can do something similar to below. If the method exists call it otherwise display your own error message.
function _remap( $method )
{
// $method contains the second segment of your URI
if(method_exists($this, $method ) )
{
$this->$method();
}
else
{
//your custom coding here
}
}

Extending Form Validation in Codeigniter call to a member function on a non-object error

I know this has been asked before, and I've looked through every answer posted, but to no avail. Basically, I am trying to extend the Codeigniter form validation library on a CI2+ project, but every single time I get the error:
Fatal error: Call to a member function get_errors_array() on a non-object
Below is my code:
application/core/CB_Form_validation.php
class CB_Form_validation extends CI_Form_validation{
function __construct($rules = array()) {
parent::__construct($rules);
log_message('debug', "CB_Form_validaiton class initialized");
}
public function get_errors_array() {
return $this->_error_array;
}
}
and then in my ajax file I have the construct etc.
public function save(){
if($this->form_validation->run() == FALSE){
}
}
and inside that if statement I have tried the following:
echo $this->form_validation->get_errors_array();
echo $this->cb_form_validation->get_errors_array();
echo get_errors_array();
I have also tried placing the CB_Form_validation.php in application/libraries as well. Just as well, I have also tried this in my construct of my ajax controller
$this->load->library('CB_Form_validation');
$this->load->library('core/CB_Form_validation');
And I have set CB_ as my custom class prefix
Turns out that to fix this, you should do the following:
Move your custom form validation class to the application/libraries folder
You can keep the construct of your custom class how it is
Use $this->form_validation->method() to access the function you would like
As long as your loading the form_validation library, you don't need to perform $this->load->library('custom_class') because CI picks up on your custom prefix anyways
Hope this helps someone

Dynamic router name for magento controller

How would I go about creating a custom module that has a controller with an action name that is dynamic, in the sense that it can be configured by the user in the admin area at will and be automatically updated in the custom module?
You can override this method in your controller:
public function getActionMethodName($action)
{
return 'indexAction';
}
public function indexAction()
{
//action name
var_dump($this->getRequest()->getActionName());
}
Then always will go to the index action, where you can use the original action name as a parameter.
then:
http://mysite/mymodule/mycontroller/im-dracula-blablabla
Will work!
I think you can approach this by using magic php method __call on your controller.
I assumed that you store your action name in a Magento config named 'mymodule/controller/action', so you can get the value using :
Mage::getStoreConfig('mymodule/controller/action');
Then you have the controller for example Mymodule/controllers/TestController.php
And you add the method in that controller like this :
public function __call($method, $arg) {
if ($method == Mage::getStoreConfig('mymodule/controller/action')) {
//Do whatever you want
}
}
This will make your controller //Do whatever you want when you accessing it using the action you specified in the config. The basic idea is like that. Hope this helps.

Resources