The Clean Architecture in php


namespace CleanPhp\Invoicer\Service\InputFilter; use



Download 2,26 Mb.
Pdf ko'rish
bet159/179
Sana24.06.2021
Hajmi2,26 Mb.
#100337
1   ...   155   156   157   158   159   160   161   162   ...   179
Bog'liq
The-Clean-Architecture-in-PHP-Kristopher-Wilson



=
$this
->
validationErrors
(
'customer.id'
)
?>



Our Application in Zend Framework 2
169

Order Number:
id="order_number" placeholder="Enter Order Number"
value="

=
$this
->
escapeHtmlAttr
(
$this
->
order
->
getOrderNumber
())
?>
">

=
$this
->
validationErrors
(
'orderNumber'
)
?>


Description:
id="description" placeholder="Enter Description"
value="

=
$this
->
escapeHtmlAttr
(
$this
->
order
->
getDescription
())
?>
">

=
$this
->
validationErrors
(
'description'
)
?>


Total:
id="total" placeholder="Enter Total"
value="

=
$this
->
escapeHtmlAttr
(
$this
->
order
->
getTotal
())
?>
">

=
$this
->
validationErrors
(
'total'
)
?>

Save

We use the
$order
object supplied by the
ViewModel
to populate the form. Of course, at this point
we don’t have any data to populate it with. We use the
$customers
to provide the select options
for the Customer drop down.
If we hit up
/orders/new
in our browser, we’ll see our hard work. When we click the
Save
button,
the page looks the same. It’s probably time to handle the POST data.
Handling POST Data
To facilitate saving the posted data, we’ll need an instance of our
OrderInputFilter
injected to
the controller so we can use it to validate the POST data, and an instance of our
OrderHydrator
to hydrate the data:
// module/Application/src/Application/Controller/OrdersController.php
namespace
Application\Controller;
use
CleanPhp\Invoicer\Domain\Entity\Order;
use
CleanPhp\Invoicer\Domain\Repository\CustomerRepositoryInterface;
use
CleanPhp\Invoicer\Domain\Repository\OrderRepositoryInterface;
use
CleanPhp\Invoicer\Persistence\Hydrator\OrderHydrator;
use
CleanPhp\Invoicer\Service\InputFilter\OrderInputFilter;


Our Application in Zend Framework 2
170
use
Zend\Mvc\Controller\AbstractActionController;
use
Zend\View\Model\ViewModel;
class
OrdersController
extends
AbstractActionController {
protected
$orderRepository
;
protected
$customerRepository
;
protected
$inputFilter
;
protected
$hydrator
;
public function
__construct
(
OrderRepositoryInterface
$orderRepository
,
CustomerRepositoryInterface
$customerRepository
,
OrderInputFilter
$inputFilter
,
OrderHydrator
$hydrator
) {
$this
->
orderRepository
=
$orderRepository
;
$this
->
customerRepository
=
$customerRepository
;
$this
->
inputFilter
=
$inputFilter
;
$this
->
hydrator
=
$hydrator
;
}
// ...
}
If this seems like a lot to inject into the controller, it just might be. A solution might be to break
this controller up into multiple controllers with smaller responsibilities/less actions. Or you could
investigate a different pattern, like Paul Jones’
Action-Domain-Responder⁴⁶
. For now, I’ll stick
with this one controller.
Next, let’s update our
controllers
config to inject in an
OrderInputFilter
:
// module/Application/config/module.config.php
use
CleanPhp\Invoicer\Service\InputFilter\OrderInputFilter;
return
[
// ...
'controllers'
=>
[
'invokables'
=>
[
'Application\Controller\Index'
=>
'Application\Controller\IndexController'
],
'factories'
=>
[
// ...
'Application\Controller\Orders'
=>
function
(
$sm
) {
⁴⁶
https://github.com/pmjones/adr


Our Application in Zend Framework 2
171
return new
\Application\Controller\OrdersController(
$sm
->
getServiceLocator
()
->
get
(
'OrderTable'
),
$sm
->
getServiceLocator
()
->
get
(
'CustomerTable'
),
new
OrderInputFilter(),
$sm
->
getServiceLocator
()
->
get
(
'OrderHydrator'
)
);
},
],
],
// ...
];
Now our
OrdersController
should have everything it needs to operate. Let’s update the
newAction()
to utilize these components to posted data:
public function
newAction
() {
$viewModel
=

Download 2,26 Mb.

Do'stlaringiz bilan baham:
1   ...   155   156   157   158   159   160   161   162   ...   179




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish