Download the PHP package sinclairt/magic-views without Composer
On this page you can find all versions of the php package sinclairt/magic-views. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sinclairt/magic-views
More information about sinclairt/magic-views
Files in sinclairt/magic-views
Package magic-views
Short Description Magic CRUD views that takes the hassle out of repetitive blades. Useful for RAD
License MIT
Informations about the package magic-views
Magic Views
Magic views are a quick and simple way to implement crud views with as little or as much customisation as you want.
Installation
Add the service provider to your
Usage
Use the trait in your controller . As it stands, there are four views: index, create, edit, and show. The edit and update views require a variable, the index view requires a variable which should be an instance of . You can get a paginated object by using on the end of your query instead of i.e.
So an index method might look something like this:
Create like this...
Edit...
Show...
Assumptions
Magic Views makes a few assumptions to ensure your views are rendered with as little configuration as possible:
-
You are using resourceful routing where your route names are the same as the model i.e. model means
-
You are following a naming convention with your models and controllers i.e. a model has a controller of
- You have registered the model in the IoC container by its short name i.e. is registered in the IoC container as
Settings
The index view will use the array and the array to decide which columns to show, but you can over ride this by setting the variable on your model:
The create and edit views use the array to display fields on the forms, these will default to text but you can over ride this by setting the variable on your model:
The key is the field name, the value is the field type.
Available field types:
- text
- plain-text
- password
- radio
- select
- checkbox
- text-area
- submit
- text-disabled
You can extend this of course. Publish the assets, and then add your own blade in the partials/form directory and list the view in the fields array i.e. let's say you need a particular input when creating a user to assign roles, so you might have a and then set , hey presto your custom input is used in the form. This can be a quicker way of adding content to your forms then using the and hooks.
Field Notes
A select requires options, set the options by passing in a variable with the naming convention of fieldnameOptions i.e. role would be . This variable should an associative array with the key as the value to submit from the form i.e. the id, and the value as the display value for the option i.e. the name.
Buttons
On the index view are some buttons: show, edit, delete, and where your model uses soft deletes restore. By default all buttons are on ( when an object is trashed only restore and show are visible, and when its not restore is hidden, and edit and delete are added. You can configure the buttons you want to show by passing in a variable with the buttons you wish to show:
Sometimes you might want to disable the creation of objects, to do this set the variable and pass into your view and this will remove the new button.
The buttons can be formatted for a drop menu instead by setting the variable and passing into your view.
Display Values
There are three ways to approach this, as the value is, using Laravel's mutators, or using a Presenter. If your happy using the first two, then there is nothing more to do, then to read up on Eloquent Mutators if necessary. If you would like to use a Presenter, Magic Views will look for this so that it can be used in the view, it will assume the method on the presenter is the field name studly cased with 'present' in front i.e. becomes . If you would like to over ride the name of the presenter method to use then pass in a variable with the naming convention of presentFieldName i.e would become ; you would then pass this a string of the method name you would like to use e.g. let's say you want to present the username instead of the user_id, and your method on your presenter is you would do the following:
Now Magic Views will load the userName method instead.
If you do not have a method in the presenter for your method it will revert to the column value or mutator if set.
Other Variables
Magic views creates the following variables to put into the view, you can over ride them by passing in the variable(s) yourself:
- breadcrumbs
- action (this is the action on forms)
- pageTitle
- pageSubTitle
- panelTitle
Additional Content
Magic Views allows you to add additional content to both the forms and the pages. To do this, create a view and pass the view name to one of the following variables and inject to the view:
-
Adds content before the form fields but inside the form
-
Adds content after the form field but inside the form
-
Adds content before the panel of content
- Adds content after the panel of content
Further Customisation
You can further customise the views by changing the config values, the language values, or the editing the views themselves. To do this publish the assets of the package by running . All values displayed outside of model and presenter values, are passed through a language file first, there is an example for users in the language file already. The term sits on the top level of the array, but the fields of the , including the column names in the index view, sit inside the fields array. Placeholders can also be set here where applicable just add onto the end of the column/field name.
If you would like to swap out any of the partials for your own, you can change the value in the config file. You can also change the title tag, brand image, nav-links, form-action assumptions, and any breadcrumb prefix you may want i.e. home or dashboard.
There is also a fallback option for language files. If you supply a dot notation string to the method in Laravel and it doesn't find a value it will return the dot notation string ( ), the logic behind this is that it is easier to spot a missing language value if it is displayed like this. If you switch on the fallback (default is true), then it will find the last element in your dot notation and return that instead of the full string i.e. becomes . If want to turn this off, change the to . The method is if you would like to use in your own code.
Beyond this, you can edit the views themselves.
Finally
You can extend the Magic Views so long as you create them. For example, lets take a stats page for a user, create a , which might extend the (or use the config value ), and add a section with your content inside. Now you can call in your controller. Or, if you publish the assets ( ), and put your view inside you can call
There are two yield methods in the layout for code to go into the and the should you need it.
All versions of magic-views with dependencies
illuminate/support Version ^5.1
illuminate/contracts Version ^5.1