Onun MVC kullanarak, Zend Framework Rota Named çok basit oluşturun

1 Cevap php

Ben sadece birlikte Zend Framework ve MVC kullanarak bir çok temel bir site koyduk. (Aslında, ben bile şu anda modellerini kullanarak değilim, hepsi şimdiye kadar statik bilgi için sadece Kontrolörleri / İzlenme bulunuyor).

Ben Forms ile oynuyor başladığımda Zend_Form için örneklerde bu formun eylemi ayarlama böyle bir şey kullanmak fark:

$form->setAction('/user/login')

Hangi URL içeriyor. Ben Zend Framework Rotalar vardır ve onlar adlandırılmış olabilir anlıyorum, ama ben böyle bir şey yapabileceğini, böylece belli Kontrolörü / Eylemler için basit bir yol oluşturmak için nasıl manuel kavramak gibi olamaz:

$form->setAction($named_route)
// or 
$form->setAction('named_route')

Benim soru açıktır umuyoruz. Ben herhangi bir yinelenen soruları bulmak mümkün değildi, ama bir nokta ve bunu işaret eğer sakıncası olmaz.

Kaynaklara bağlantılar örnekler kadar iyi, bu yüzden bir yerde iyi bir blog yazısı biliyorsanız eğer zamanınızı boşa harcamayın. Teşekkürler!

1 Cevap

References: http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard - Look for "12.5.7.1. Zend_Controller_Router_Route" for a clearer explanation.

This is not the best way to do it, but I have a working example. I welcome corrections. After seeing Shorten Zend Framework Route Definitions, I agree that named Routes should go in their own config (I use Django, and named Views/URLs are generally separated) - but here I'm just going to define Routes in my Bootstrap.

Yani, bootstrap.php, tabii ki Bootstrap Sınıf içinde, öyle gibi, otomatik olarak çalışacak bir fonksiyon oluşturduk:

public function _initRoutes()
{
    $frontController = Zend_Controller_Front::getInstance();
    $route = new Zend_Controller_Router_Route(
        'login/', // The URL, after the baseUrl, with no params.
        array(
            'controller' => 'login', // The controller to point to.
            'action'     => 'index'  // The action to point to, in said Controller.
        )
    );
    $frontController->getRouter()->addRoute('loginpage', $route);
}

Yukarıdaki örnekte, "loginpage" "Named Yolu" nun adı olacaktır.

Yani, benim LoginController içinde, (formu oluşturur bir işlevi) yerine yapıyor

$form->setAction('/blah/login')

Ben adlı Route URL almak ve böylece gibi, o geçmek:

$form_action_url = $this->view->Url(array(), 'loginpage', true);
// -- SNIP --
$form->setAction($form_action_url) // ...

This may be pointless and wrong, but it seems to work at the moment. My reason for wanting a named URL, when Zend Framework handles URLs as /Controller/View(Action)/ automatically is because I'm anal about that kind of thing. I've been using Django for awhile, where the URLs are predefined, and I like it that way.

Kutunun dışında çalışma Zend Framework MVC adresler Tho, güzel.

Bu nasıl çalışması gerektiğini notlar ve düzeltmeler eklemek için çekinmeyin!