3
3
namespace Ajax \php \cakephp ;
4
4
5
5
6
- use Ajax \service \JString ;
7
6
use Cake \Routing \Router ;
7
+ use Cake \View \View ;
8
+ use Cake \Network \Response ;
9
+ use Cake \Core \App ;
10
+
8
11
class _JsUtils extends \Ajax \JsUtils{
9
12
public function getUrl ($ url ){
10
13
return Router::url ($ url );
@@ -25,32 +28,39 @@ public function createScriptVariable(&$view,$view_var, $output){
25
28
}
26
29
27
30
/**
28
- * @param Symfony\Component\DependencyInjection\ContainerInterface $initialControllerInstance
31
+ * @param App\Controller\AppController $initialControllerInstance
29
32
* @param string $controllerName
30
33
* @param string $actionName
31
34
* @param array $params
32
35
* @see \Ajax\JsUtils::forward()
33
36
*/
34
37
public function forward ($ initialControllerInstance ,$ controllerName ,$ actionName ,$ params =array ()){
35
- $ path =$ params ;
36
- $ request = $ initialControllerInstance ->get ('request_stack ' )->getCurrentRequest ();
37
- $ path ['_forwarded ' ] = $ request ->attributes ;
38
- $ path ['_controller ' ] = $ controllerName .": " .$ actionName ;
39
- $ subRequest = $ request ->duplicate ([], null , $ path );
40
- $ response = $ initialControllerInstance ->get ('http_kernel ' )->handle ($ subRequest , HttpKernelInterface::SUB_REQUEST );
41
- return $ response ->getContent ();
38
+ \ob_start ();
39
+ if (isset ($ params ) && \is_array ($ params )===false ){
40
+ $ params =[$ params ];
41
+ }
42
+ $ url =h (Router::url (\array_merge ([
43
+ 'controller ' => $ controllerName ,
44
+ 'action ' => $ actionName ],$ params ),false
45
+ ));
46
+ $ base =Router::url ("/ " );
47
+ if (substr ($ url , 0 , strlen ($ base )) == $ base ) {
48
+ $ url = substr ($ url , strlen ($ base ));
49
+ }
50
+ $ initialControllerInstance ->requestAction ($ url );
51
+ $ result =\ob_get_contents ();
52
+ \ob_end_clean ();
53
+ return $ result ;
42
54
}
43
55
44
56
public function renderContent ($ initialControllerInstance ,$ viewName , $ params =NULL ) {
45
- if ($ initialControllerInstance ->has ('templating ' )) {
46
- return $ initialControllerInstance ->get ('templating ' )->render ($ viewName , $ params );
47
- }
48
-
49
- if (!$ initialControllerInstance ->has ('twig ' )) {
50
- throw new \LogicException ('You can not use the "renderView" method if the Templating Component or the Twig Bundle are not available. ' );
51
- }
52
-
53
- return $ initialControllerInstance ->get ('twig ' )->render ($ viewName , $ params );
57
+ $ view = new View (Router::getRequest (true ), new Response ());
58
+ if (\is_array ($ params )){
59
+ foreach ($ params as $ k =>$ v ){
60
+ $ view ->set ($ k , $ v );
61
+ }
62
+ }
63
+ return $ view ->render ($ viewName );
54
64
}
55
65
56
66
public function fromDispatcher ($ dispatcher ){
0 commit comments