creating a cli interface to mvc web app

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

creating a cli interface to mvc web app

jackle
I wanted to create a scriptable cli to a web app that we have have. One of the things that it does is that it kicks off a set of writing thousands of static html files.

I have something like the following:
$fc=Zend_Controller_Front::getInstance();
$fc->setControllerDirectory(array(
                     'default'=>$app.'/controllers/'));

Zend_Layout::startMvc(array('layoutPath' => $app.'/layouts/'));

$request=new Zend_Controller_Request_Simple('jt','generator');
$response=new Zend_Controller_Response_Cli();

$fc->setRequest($request);
$fc->setResponse($response);
$fc->throwExceptions(true);
Zend_Debug::dump($request);
$fc->dispatch();

but it gives me the following error: 
PHP Fatal error:  Uncaught exception 'Zend_Controller_Router_Exception' with message 'Zend_Controller_Router_Rewrite requires a Zend_Controller_Request_Http-based request object' in /data/sites/site/www/zf_source/library/Zend/Controller/Router/Rewrite.php:359

Do I need to call this upon the index.php that I have in my doc_root? 

I'm sure somebody has crossed this bridge so I greatly appreciate the help.

-jonathan


Reply | Threaded
Open this post in threaded view
|

Re: creating a cli interface to mvc web app

padraicb
I posted a blog article about this just yesterday. The problem is that routing is a built in requirement of the FrontController dispatch cycle and there's no way to disable it, and yes, by default it insists on complaining about CLI requests.

You can wrok around it by supplying the FrontController with a custom Router which basically stubs all the right methods a Router needs, but which are all empty. So the FC can do routing, but the Router is just a dummy class that does nothing. Messy, but works ;). Here's the class I'm using:

<?php

class ZFExt_Controller_Router_Cli implements Zend_Controller_Router_Interface
{

    public function route(Zend_Controller_Request_Abstract $dispatcher){}
    public function assemble($userParams, $name = null, $reset = false, $encode = true){}
    public function getFrontController(){}
    public function setFrontController(Zend_Controller_Front $controller){}
    public function setParam($name, $value){}
    public function setParams(array $params){}
    public function getParam($name){}
    public function getParams(){}
    public function clearParams($name = null){}

}
 
Pádraic Brady

http://blog.astrumfutura.com
http://www.survivethedeepend.com
OpenID Europe Foundation Irish Representative



From: water <[hidden email]>
To: [hidden email]
Sent: Wed, September 30, 2009 9:35:55 PM
Subject: [fw-mvc] creating a cli interface to mvc web app

I wanted to create a scriptable cli to a web app that we have have. One of the things that it does is that it kicks off a set of writing thousands of static html files.

I have something like the following:
$fc=Zend_Controller_Front::getInstance();
$fc->setControllerDirectory(array(
                     'default'=>$app.'/controllers/'));

Zend_Layout::startMvc(array('layoutPath' => $app.'/layouts/'));

$request=new Zend_Controller_Request_Simple('jt','generator');
$response=new Zend_Controller_Response_Cli();

$fc->setRequest($request);
$fc->setResponse($response);
$fc->throwExceptions(true);
Zend_Debug::dump($request);
$fc->dispatch();

but it gives me the following error: 
PHP Fatal error:  Uncaught exception 'Zend_Controller_Router_Exception' with message 'Zend_Controller_Router_Rewrite requires a Zend_Controller_Request_Http-based request object' in /data/sites/site/www/zf_source/library/Zend/Controller/Router/Rewrite.php:359

Do I need to call this upon the index.php that I have in my doc_root? 

I'm sure somebody has crossed this bridge so I greatly appreciate the help.

-jonathan


Reply | Threaded
Open this post in threaded view
|

Re: creating a cli interface to mvc web app

jackle
Great thanks again. I have been thinking bypassing Zend's MVC and just having a class that calls a service that writes out the static pages.

The service would essentially have to manage the Zend_Layout and Zend_View pieces. something like this:

class Generator{

    public function generatePage(){
        $file='/data/sites/site.com/www/docs/test_jt.html';
        $layout=new Zend_Layout();
        $layout->setLayoutPath('/data/sites/site.com/www/zf/app/layouts');
        $layout->setLayout('test');

   
        $content_view=new Zend_View();
        $content_view->setScriptPath('/data/sites/site.com/www/zf/app/views/scripts');
        $site_view=$content_view->render('partials/jt.phtml');
        $layout->content=$site_view;
        $data=$layout->render();
        file_put_contents($file, $data);
    }
}

But obviously, the MVC magic is gone for things like the layout helper allowing things like echo'ing partials within layouts. Is there a way to allow within my layout:
<?php
echo $this->parital('partials/a_sample_partial.phtml');
?>

thanks for any help.

-j

From: Pádraic Brady <[hidden email]>
To: water <[hidden email]>
Cc: Zend Framework MVC <[hidden email]>
Sent: Wednesday, September 30, 2009 3:59:56 PM
Subject: Re: [fw-mvc] creating a cli interface to mvc web app

I posted a blog article about this just yesterday. The problem is that routing is a built in requirement of the FrontController dispatch cycle and there's no way to disable it, and yes, by default it insists on complaining about CLI requests.

You can wrok around it by supplying the FrontController with a custom Router which basically stubs all the right methods a Router needs, but which are all empty. So the FC can do routing, but the Router is just a dummy class that does nothing. Messy, but works ;). Here's the class I'm using:

<?php

class ZFExt_Controller_Router_Cli implements Zend_Controller_Router_Interface
{

    public function route(Zend_Controller_Request_Abstract $dispatcher){}
    public function assemble($userParams, $name = null, $reset = false, $encode = true){}
    public function getFrontController(){}
    public function setFrontController(Zend_Controller_Front $controller){}
    public function setParam($name, $value){}
    public function setParams(array $params){}
    public function getParam($name){}
    public function getParams(){}
    public function clearParams($name = null){}

}
 
Pádraic Brady

http://blog.astrumfutura.com
http://www.survivethedeepend.com
OpenID Europe Foundation Irish Representative



From: water <[hidden email]>
To: [hidden email]
Sent: Wed, September 30, 2009 9:35:55 PM
Subject: [fw-mvc] creating a cli interface to mvc web app

I wanted to create a scriptable cli to a web app that we have have. One of the things that it does is that it kicks off a set of writing thousands of static html files.

I have something like the following:
$fc=Zend_Controller_Front::getInstance();
$fc->setControllerDirectory(array(
                     'default'=>$app.'/controllers/'));

Zend_Layout::startMvc(array('layoutPath' => $app.'/layouts/'));

$request=new Zend_Controller_Request_Simple('jt','generator');
$response=new Zend_Controller_Response_Cli();

$fc->setRequest($request);
$fc->setResponse($response);
$fc->throwExceptions(true);
Zend_Debug::dump($request);
$fc->dispatch();

but it gives me the following error: 
PHP Fatal error:  Uncaught exception 'Zend_Controller_Router_Exception' with message 'Zend_Controller_Router_Rewrite requires a Zend_Controller_Request_Http-based request object' in /data/sites/site/www/zf_source/library/Zend/Controller/Router/Rewrite.php:359

Do I need to call this upon the index.php that I have in my doc_root? 

I'm sure somebody has crossed this bridge so I greatly appreciate the help.

-jonathan