class
CustomersController
extends
AbstractActionController {
// ...
public function
newOrEditAction
() {
// ...
}
}
Next, we’ll update the routing config to point to this new action, and also add the edit action
while we’re at it:
// module/Application/config/module.config.php
return
[
'router'
=>
[
'routes'
=>
[
// ...
'customers'
=>
[
'type'
=>
'Segment'
,
'options'
=>
[
/* ... */
],
'may_terminate'
=>
true
,
'child_routes'
=>
[
Our Application in Zend Framework 2
151
'new'
=>
[
'type'
=>
'Segment'
,
'options'
=>
[
'route'
=>
'/new'
,
'constraints'
=>
[
'id'
=>
'[0-9]+'
,
],
'defaults'
=>
[
'action'
=>
'new-or-edit'
,
],
]
],
'edit'
=>
[
'type'
=>
'Segment'
,
'options'
=>
[
'route'
=>
'/edit/:id'
,
'constraints'
=>
[
'id'
=>
'[0-9]+'
,
],
'defaults'
=>
[
'action'
=>
'new-or-edit'
,
],
]
],
]
],
// ...
],
],
];
Finally, let’s rename our
view/application/customers/new-or-edit.phtml
file to
module/Application/view/application/customers/new-or-edit.phtml
. At this point, our Cre-
ate Customer button and action should still work. If we click on the id of a row in the
indexAction()
, we should also get a form in the browser, just missing our data. Let’s fix that.
The first thing we’ll want to do is check for an ID passed via the URL. If we have one, we should
get a
Customer
object from the
CustomerRepository
. If there is no ID, we should instantiate a
new
Customer
object just like we currently are:
Our Application in Zend Framework 2
152
public function
newOrEditAction
() {
$id
=
$this
->
params
()
->
fromRoute
(
'id'
);
$customer
=
$id
?
$this
->
customerRepository
->
getById
(
$id
)
:
new
Customer();
// ...
}
This simple change should be all we need to support editing Customers. Give it a try. Sweet,
huh?
We want to do two more things:
1. Link to the Edit Customer page after a successful save.
2. Show Edit Customer as the title instead of New Customer when editing
The first one is easy; we change the redirect line in
newOrEditAction()
to:
$this
->
redirect
()
->
toUrl
(
'/customers/edit/'
.
$customer
->
getId
());
And changing the title in the view is pretty easy, too:
<
div class
=
"page-header clearfix"
>
<
h2
>
= !
empty
(
$this
->
customer
->
getId
())
?
'Edit'
:
'New'
?>
Customer
We simply check to see if the
$customer
has an ID to determine if it is an edit or add operation.
Customer Management is now complete!
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
06-editing-customers:
git
clone
https
://
github
.
com
/
mrkrstphr
/
cleanphp
-
example
.
git
git checkout
06-
editing
-
customers
Our Application in Zend Framework 2
153
Order Management
Let’s move on to orders. We’re going to start by hand-crafting a List Orders view, much the same
way we created a List Customers view. We already defined our basic route for
/orders
earlier
in this chapter, so let’s continue that by creating our controller that will be served by this route.
For our
indexAction()
, we simply want to get an a collection of all Orders stored within the
database. The controller will use an implementation of the
OrderRepositoryInterface
, injected
via the constructor, and it’s
getAll()
method to get the orders.
// module/Application/src/Application/Controller/OrdersController.php
Do'stlaringiz bilan baham: |