The Clean Architecture in php


namespace Application\Controller; use



Download 2,26 Mb.
Pdf ko'rish
bet153/179
Sana24.06.2021
Hajmi2,26 Mb.
#100337
1   ...   149   150   151   152   153   154   155   156   ...   179
Bog'liq
The-Clean-Architecture-in-PHP-Kristopher-Wilson

namespace
Application\Controller;
use
CleanPhp\Invoicer\Domain\Repository\CustomerRepositoryInterface;
use
Zend\Mvc\Controller\AbstractActionController;
class
CustomersController
extends
AbstractActionController {
public
$customerRepository
;
public function
__construct
(
CustomerRepositoryInterface
$customers
) {
$this
->
customerRepository
=
$customers
;
}
public function
indexAction
() {
return
[
'customers'
=>
$this
->
customerRepository
->
getAll
()
];
}
}
We have a new
CustomersController
class with an
indexAction()
method. An instance of
CustomerRepositoryInterface
is injected in, and later used by the action to call the
getAll()
method. We return the result of that method in an array, keyed at
customers
.
Now we need a proper view to represent the
indexAction()
, and we should see our data on the
screen. Let’s drop that view file in:
module
/
Application
/
views
/
application
/
customers
/
index
.
phtml
-->
<
div class
=
"page-header clearfix"
>
<
h2 class
=
"pull-left"
>
Customers
h2
>
<
a href
=
"/customers/new"
class
=
"btn btn-success pull-right"
>
Create Customer
a
>
div
>
<
table class
=
"table"
>
<
thead
>
<
tr
>
<
th
>
#
<
th
>
Name
th
>
<
th
>
Email
th
>
tr
>
thead
>


Our Application in Zend Framework 2
135
php
foreach
(
$this
->
customers
as
$customer
)
:
?>






endforeach
;
?>


=
$customer
->
getId
()
?>
">

=
$customer
->
getId
()
?>


=
$customer
->
getName
()
?>


=
$customer
->
getEmail
()
?>

Lastly, we’ll need to configure ZF2 to know that
CustomersController
is the
Customers
controller we referenced in the route. If if we had called it
CustomersController
in the route,
ZF2 still wouldn’t know what we’re talking about as the string here is simply the key within the
controller service locator.
In the
controllers
section of the module config file, we’ll add an entry for our new controller:
// module/Application/config/module.config.php
return
[
// ...
'controllers'
=>
[
'invokables'
=>
[
'Application\Controller\Index'
=>
'Application\Controller\IndexController'
],
'factories'
=>
[
'Application\Controller\Customers'
=>
function
(
$sm
) {
return new
\Application\Controller\CustomersController(
$sm
->
getServiceLocator
()
->
get
(
'CustomerTable'
)
);
},
],
],
// ...
];
Unlike the main
IndexController
, this
CustomersController
entry will be registered with ZF
as a factory, so that it’s not just instantiated outright, but allows us to bake in logic about
how it’s instantiated, which allows us to inject the proper dependencies. We’re using the entry
we defined in the last chapter for
CustomerTable
to grab our Customer Data Table, which
implements the
CustomerRepositoryInterface
and satisfies the type-hint on the constructor
of the
CustomersController
.


Our Application in Zend Framework 2
136
So now if we navigate to
/customers
in our beloved browser, we should see all of our customers
from our sqlite database rendered on to the screen. Success!
This would make a good place to commit your code to source control.
If you’re just reading, but want to see the code in action, you can checkout the tag
05-viewing-customers:
git
clone
https
://
github
.
com
/
mrkrstphr
/
cleanphp
-
example
.
git
git checkout
05-
viewing
-
customers
Creating Customers
In our HTML, we have a button for creating new customers that brings the user to the route
/customers/new
. At this route, we’ll render a form that, when correctly filled out, will then
post back to the same route where we’ll persist the new information to the database as a new
customer.
Let’s start building out our
CustomersController->newAction()
to handle simple GET requests.

Download 2,26 Mb.

Do'stlaringiz bilan baham:
1   ...   149   150   151   152   153   154   155   156   ...   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