Download the PHP package mikehaertl/xcrudcontroller without Composer
On this page you can find all versions of the php package mikehaertl/xcrudcontroller. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mikehaertl/xcrudcontroller
More information about mikehaertl/xcrudcontroller
Files in mikehaertl/xcrudcontroller
Package xcrudcontroller
Short Description xcrudcontroller is a Yii base class to quickly build customized CRUD interfaces.
License MIT
Homepage https://github.com/mikehaertl/xcrudcontroller
Informations about the package xcrudcontroller
XCrudController
A base class to quickly build customized CRUD interfaces.
Features
- Create, list, view, edit and delete actions. Follow DRY and don't repeat the same CRUD logic in all your controllers.
- Powerful filter pattern. Use a dedicated filter form in your listing to build convenient search interfaces.
- Helpers for URL creation. Take the user back to the same listing page after editing/viewing a record.
Quickstart
1 A simple controller
To get started you only have to supply the name of the ActiveRecord class that you want to build a CRUD for:
2 Create view files
You have to supply the list
, edit
and detail
views.
Note: The markup is very reduced. In a real life project you'd of course style these elements apropriately. You could also start from the CRUD templates as generated from Gii. But you have to rearrange and modify them a little.
2.1 List view
The list
view contains the filter form and renders the partial view for
the actual items via _items
.
protected/views/user/list.php:
Note: In real life your filter form is usually offering way more search options. You would better implement this through a dedicated filter form. See below for details.
protected/views/user/_items.php:
Notice how the links to view and edit an item are built through createItemUrl()
. This
adds the current list page URL as URL parameter so that it's very easy to link back
to this search result page.
2.2 Create/edit form
protected/views/user/form.php:
Note: The form name must be
strtolower($this->modelName).'-form'
if you want to use AJAX validation withCActiveForm
.
2.3 Detail view
protected/views/user/form.php:
Configuration
Views
As you've seen above, 4 view files must be provided for the controller:
list
: Contains the filter form and renders the_items
partial_items
: Renders the items (e.g. withCListView
/CGridView
)form
: Renders the create/edit formdetail
: Renders the detail view
The view names can be configured in $listView
, $listPartial
, $formView
and $detailView
properties.
The views use a pull mechanism to fetch their data:
$this->model
: Returns the current model forform
anddetail
view$this->filterModel
: Returns the filter model forlist
and_items
view. The data provider for the items is available through thesearch()
method.$this->returnUrl
: Contains the URL which takes the user back to the search result page. This is useful onedit
andview
pages.
After a record was created or updated a flash message with the key $modelName-created
or
$modelName-updated
is set respectively. You can use this to show a success message
in the view which you show after saving a record.
Note: You can control, which page you want to show after you created a new record. By default this will be the list page (with filters reset). You can also show the detail view of the new record or stay on the edit page, if you want. Therefore you have to create the "Add" link like this:
CHtml::link('Add user', array('edit', $this->returnVar=>'view'));
Instead of 'view' you can also use 'edit' to redirect to the edit form of the newly created record.
Scenarios
You can configure several scenarios which are set on the $model
or $filterModel
for
specific actions:
$createScenario
: Set when creating a new record. Default iscreate
.$updateScenario
: Set when updating a record. Default isupdate
.$filterScenario
: Set on the filter model when assigning search parameters. Default isfilter
.
Other options
If you don't want to provide all actions, you can disable some of them through
$crudActions
. By default this is `array('edit', 'list', 'view', 'delete').
The name of the URL parameter for the return URL is set through $returnVar
and defaults
to returnUrl
.
Advanced tricks
Using a filter model
In the simplest case a filter model is an ActiveRecord as created by Gii. But if you want to keep your code cleaner, i'd recommend to use a separate model file for all your filter concerns. This has the advantage that you don't clutter up your ActiveRecords with search related code anymore and can focus on the search logic here:
In the controller you have to configure this model in $filterModelName
:
How to get nicer URLs for your search results
The URL parameters to a search result page look like this:
...&User[username]=test&User[email][email protected]
This does not look very nice. It would be much better to have easier to read search parameters, just like Google or other search engines do it:
...&username=test&[email protected]
It's very easy to achieve this. You have to override the assignFilterModelAttributes()
method in your controller like this:
It is safe to do this in this case: We only have the search form parameters in $_GET, plus maybe the pagination and sorting options. But as long as they don't interfere with your search model attributes everything is still fine.
But you also have to change the filter form a little because by default Yii
will create form element names like User[username]
whereas we want them
to be only username
now: