$view->action points to parent page breaking ajax calls
Permalink
I wrote a custom single page which reads 2 additional parameters off the url
Typical URL: /index.php/mysinglepage/88/138
This single page also allows additional blocks to be installed on it. However, when using $view->action("myAjaxfunction") to create an ajax route to the block's controller, it is actually being interrupted as parameters for the single page controller.
So, the $view creates a URL that looks like this
"http://mywebsite/index.php/mysinglepage/myAjaxfunction/205";
So obviously it get trapped by the single page and processed in the view function:
When what i'm hoping for is:
Am I not using $view->action("myAjaxfunction") correctly or should my single page have code to pass calls from blocks back to the block's controller?
Thanks for your help!!!
Typical URL: /index.php/mysinglepage/88/138
class mysinglepage extends PageController{ public function view($pram1 = null, $pram2 = null){ //$pram1 = 88 //$pram2 = 138 } }
This single page also allows additional blocks to be installed on it. However, when using $view->action("myAjaxfunction") to create an ajax route to the block's controller, it is actually being interrupted as parameters for the single page controller.
So, the $view creates a URL that looks like this
"http://mywebsite/index.php/mysinglepage/myAjaxfunction/205";
So obviously it get trapped by the single page and processed in the view function:
class mysinglepage extends PageController{ public function view($pram1 = null, $pram2 = null){ //$pram1 = "myAjaxfunction" //$pram2 = 205 (bID) } }
When what i'm hoping for is:
class Controller extends BlockController{ public function action_myAjaxfunction($bID = false){ } }
Am I not using $view->action("myAjaxfunction") correctly or should my single page have code to pass calls from blocks back to the block's controller?
Thanks for your help!!!
Life can be so simple when you know all the answers!
Thanks!
Thanks!
Hmmm... I'm sorry, that actually did not fix the problem.
It looks like $view & $this return the same action URL.
The block works correctly on other pages. But when placed on my custom single page (which needs to read some parameters) the ajax request is being intercepted by the single page controller file.
I even tested on other pages which have URL parameters similar to mine (I.E. /members/profile/view/2)
I'm guessing my problem is in my single page... Is this the correct way to grab stuff off the URL?
I really do not want to use the traditional get approach (?fileID=xxx&userID=xxx")
When the block tries to make the ajax call to "/index.php/mySinglePage/myAjaxFinction/207" is is picked up by the single page's view function.
Thanks again!
It looks like $view & $this return the same action URL.
The block works correctly on other pages. But when placed on my custom single page (which needs to read some parameters) the ajax request is being intercepted by the single page controller file.
I even tested on other pages which have URL parameters similar to mine (I.E. /members/profile/view/2)
I'm guessing my problem is in my single page... Is this the correct way to grab stuff off the URL?
class mySinglePage extends PageController { public function view($fileID = null, $userID = null) ... }
I really do not want to use the traditional get approach (?fileID=xxx&userID=xxx")
When the block tries to make the ajax call to "/index.php/mySinglePage/myAjaxFinction/207" is is picked up by the single page's view function.
Thanks again!
it's not clear to me what you want to do now
1. block view -> block controller
2. block view -> singlepage controller
3. singlepage view -> singlepage controller
I thought you wanted 1, but you say
'When the block tries to make the ajax call to "/index.php/mySinglePage/myAjaxFinction/207"´
which indicates 2
1. block view -> block controller
2. block view -> singlepage controller
3. singlepage view -> singlepage controller
I thought you wanted 1, but you say
'When the block tries to make the ajax call to "/index.php/mySinglePage/myAjaxFinction/207"´
which indicates 2
Sorry for the confusion.
1. is what i want.
The ajax calls (made in the blocks view.js) get intercepted by my single page's controller file. I'm expecting the call to be routed to me block's controller file.
Just ran a test...
1. Loaded the single page containing the block...
2. Ran the ajax call (was caught by the single page's controller)
3. Without refreshing the browser, I changed the single page's controller from:
public function view($fileID = null, $blockcID = null)
to:
public function view()
4. Ran the ajax call again and it correctly routed ajax to the block's controller.
So, I am thinking the problem is in my single page.
1. is what i want.
The ajax calls (made in the blocks view.js) get intercepted by my single page's controller file. I'm expecting the call to be routed to me block's controller file.
Just ran a test...
1. Loaded the single page containing the block...
2. Ran the ajax call (was caught by the single page's controller)
3. Without refreshing the browser, I changed the single page's controller from:
public function view($fileID = null, $blockcID = null)
to:
public function view()
4. Ran the ajax call again and it correctly routed ajax to the block's controller.
So, I am thinking the problem is in my single page.
when creating the route for my block's ajax call, i only get one option...
"/index.php/mySinglePage/myAjaxFunction/207"
It is the route given by both $view->action("myAjaxFunction") and $this->action("myAjaxFunction").
If the block is installed on another page, the path to that page is given.
The problem is, other pages will pass the action call to the block's controller... my custom single page captures the ajax call and does not pass it along to the block's controller page.
"/index.php/mySinglePage/myAjaxFunction/207"
It is the route given by both $view->action("myAjaxFunction") and $this->action("myAjaxFunction").
If the block is installed on another page, the path to that page is given.
The problem is, other pages will pass the action call to the block's controller... my custom single page captures the ajax call and does not pass it along to the block's controller page.
mySinglePage shouldnt be in that url
I don't know how you do it...can't reproduce it. Maybe you are extending a page instead of a block for your block controller class, I dunno
A block on a single page works as good as on a normal page
I don't know how you do it...can't reproduce it. Maybe you are extending a page instead of a block for your block controller class, I dunno
A block on a single page works as good as on a normal page
here is some code to test your block's view.php:
It should give a 404 error (on firebug) cause it can't find the function until you create one in your block's controller.
<div class="myfunction btn btn-primary"> <?php echo $myUrl = $this->action('MyFunction') ?> </div> <script> $(".myfunction").on("click", function () { doAjax(); }); doAjax = function() { $.ajax({ dataType: "json", type: "post", url: "<?php echo $myUrl ?>", data: { name: "world" } , success: function (ret) { console.log("ret", ret);
Viewing 15 lines of 19 lines. View entire code block.
It should give a 404 error (on firebug) cause it can't find the function until you create one in your block's controller.
Thanks for sticking with me on this!
I put your code in my view.php for the block and added this to the block controller
Then modified the page controller's view() function
The console window then displays:
if I change the page controller to look like this
The console window then displays:
If you would like to see the page yourself - I upload everything to a development site - here:
http://dev.gallerygig.com/index.php/spotlight/41/184...
I put your code in my view.php for the block and added this to the block controller
Then modified the page controller's view() function
The console window then displays:
ret Object {ret: "Hello from block controller"}
if I change the page controller to look like this
public function view() { }
The console window then displays:
ret Object {ret: "Hello from page controller"}
If you would like to see the page yourself - I upload everything to a development site - here:
http://dev.gallerygig.com/index.php/spotlight/41/184...
Interesting
I tested this on a 5.7.5rc1 and get the opposite result
page controller view.php:
gives a block answer
page controller view.php:
gives a page answer
however:
will not give a block answer
I think the magic happens in PageController.php / setupRequestActionAndParameters
Anyways, if you wanna use view(with params), you will probably have to call the block's controller yourself, and the handler you are seeking.
But then better use Route
In your package controller (I did it in the single page package controller, but i think you could better do it in your block's package controller):
In your block's view.php you can now do:
if you wanna use params, you can register '/MyFunction{param}'
here's a nice link:http://c5hub.com/learning/ajax-57-style/...
I tested this on a 5.7.5rc1 and get the opposite result
page controller view.php:
public function view() { do stuff }
page controller view.php:
public function view($fileID = null, $blockcID = null) { switch ...}
however:
will not give a block answer
I think the magic happens in PageController.php / setupRequestActionAndParameters
Anyways, if you wanna use view(with params), you will probably have to call the block's controller yourself, and the handler you are seeking.
But then better use Route
In your package controller (I did it in the single page package controller, but i think you could better do it in your block's package controller):
on_start() { Route::register('/MyFunction', '\Concrete\Package\YourPackage\Block\YourBlock\Controller::action_MyFunction'); // or leave the action_bit }
In your block's view.php you can now do:
<?php echo $myUrl = URL::to('/MyFunction') //$this->action('MyFunction') ?>
if you wanna use params, you can register '/MyFunction{param}'
here's a nice link:http://c5hub.com/learning/ajax-57-style/...
As you may have already assumed... I got my results backwards in my last post... page<-->block. thank you for trying it in your environment.
I will give your suggestions a go and report back.
An interesting point here is the block works correctly when installed on a core single pages which use view() parameters (I.E. /members/profile/view/4).
I will give your suggestions a go and report back.
An interesting point here is the block works correctly when installed on a core single pages which use view() parameters (I.E. /members/profile/view/4).
Thanks WillemAnchor! that works.
in the block view.php use:
$this->action('myFunction');
in the block controller.php use:
public function action_myFunction() {}