├── LaravelCollectiveDocs-banner.png
├── annotations.md
├── docs.md
├── html.md
├── readme.md
└── ssh.md
/LaravelCollectiveDocs-banner.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LaravelCollective/docs/20d46f3fd402c1337cadf3e188f7d40717e59911/LaravelCollectiveDocs-banner.png
--------------------------------------------------------------------------------
/annotations.md:
--------------------------------------------------------------------------------
1 | # Annotations
2 |
3 | - [Installation](#installation)
4 | - [Scanning](#scanning)
5 | - [Event Annotations](#events)
6 | - [Route Annotations](#routes)
7 | - [Scanning Controllers](#controllers)
8 | - [Model Annotations](#models)
9 | - [Custom Annotations](#custom-annotations)
10 |
11 |
12 | ## Installation
13 |
14 | > If you have changed the top-level namespace to something like 'MyCompany', then you would use the new namespace instead of 'App'.
15 |
16 | Begin by installing this package through Composer. Edit your project's `composer.json` file to require `laravelcollective/annotations`.
17 |
18 | "require": {
19 | "laravelcollective/annotations": "6.0.\*"
20 | }
21 |
22 | Next, update Composer from the Terminal:
23 |
24 | composer update
25 |
26 | Once composer is done, you'll need to create a Service Provider in `app/Providers/AnnotationsServiceProvider.php`.
27 |
28 | ```php
29 | [
86 | // ...
87 | App\Providers\AnnotationsServiceProvider::class
88 | // ...
89 | ];
90 | ```
91 |
92 |
93 | ## Setting up Scanning
94 |
95 | Add event handler classes to the `protected $scanEvents` array to scan for event annotations.
96 |
97 | ```php
98 | /**
99 | * The classes to scan for event annotations.
100 | *
101 | * @var array
102 | */
103 | protected $scanEvents = [
104 | App\Handlers\Events\MailHandler::class,
105 | ];
106 | ```
107 |
108 | Add controllers to the `protected $scanRoutes` array to scan for route annotations.
109 |
110 | ```php
111 | /**
112 | * The classes to scan for route annotations.
113 | *
114 | * @var array
115 | */
116 | protected $scanRoutes = [
117 | App\Http\Controllers\HomeController::class,
118 | ];
119 | ```
120 |
121 | Add models to the `protected $scanModels` array to scan for model annotations.
122 |
123 | ```php
124 | /**
125 | * The classes to scan for model annotations.
126 | *
127 | * @var array
128 | */
129 | protected $scanModels = [
130 | 'App\User',
131 | ];
132 | ```
133 |
134 | Alternatively, you can set `protected $scanEverything` to `true` to automatically scan all classes within your application's namespace. *Note:* This may increase the time required to execute the scanners, depending on the size of your application.
135 |
136 | Scanning your event handlers, controllers, and models can be done manually by using `php artisan event:scan`, `php artisan route:scan`, or `php artisan model:scan` respectively. In the local environment, you can scan them automatically by setting `protected $scanWhenLocal = true`.
137 |
138 |
139 | ## Event Annotations
140 |
141 | ### @Hears
142 |
143 | The `@Hears` annotation registers an event listener for a particular event. Annotating any method with `@Hears("SomeEventName")` will register an event listener that will call that method when the `SomeEventName` event is fired.
144 |
145 | ```php
146 |
165 | ## Route Annotations
166 |
167 | Route annotations can be incredibly powerful, however the order of your route definitions can impact how your application matches specific routes, specifically any wildcard routes. If `protected $scanEverything` is set to `true`, you will have no control over the order of your route definitions.
168 |
169 | ### @Get
170 |
171 | The `@Get` annotation registeres a route for an HTTP GET request.
172 |
173 | ```php
174 | auth->logout();
262 |
263 | return redirect( route('login') );
264 | }
265 |
266 | }
267 | ```
268 |
269 | ### @Resource
270 |
271 | Using the `@Resource` annotation on a controller allows you to easily set up a Resource Controller.
272 |
273 | ```php
274 |
314 | ## Scan the Controllers Directory
315 |
316 | To recursively scan the entire controllers namespace ( `App\Http\Controllers` ), you can set the `$scanControllers` flag to true.
317 |
318 | It will automatically adjust `App` to your app's namespace.
319 |
320 | ```php
321 | $scanControllers = true;
322 | ```
323 |
324 | ### Advanced
325 |
326 | If you want to use any logic to add classes to the list to scan, you can override the `routeScans()` or `eventScans()` methods.
327 |
328 | The following is an example of adding a controller to the scan list if the current environment is `local`:
329 |
330 | ```php
331 | public function routeScans() {
332 | $classes = parent::routeScans();
333 |
334 | if ( $this->app->environment('local') ) {
335 | $classes = array_merge($classes, [App\Http\Controllers\LocalOnlyController::class]);
336 | }
337 |
338 | return $classes;
339 | }
340 | ```
341 |
342 | #### Scanning Namespaces
343 |
344 | You can use the `getClassesFromNamespace( $namespace )` method to recursively add namespaces to the list. This will scan the given namespace. It only works for classes in the `app` directory, and relies on the PSR-4 namespacing standard.
345 |
346 | This is what the `$scanControllers` flag uses with the controllers directory.
347 |
348 | Here is an example that builds on the last one - adding a whole local-only namespace.
349 |
350 | ```php
351 | public function routeScans() {
352 | $classes = parent::routeScans();
353 |
354 | if ( $this->app->environment('local') ) {
355 | {
356 | $classes = array_merge(
357 | $classes,
358 | $this->getClassesFromNamespace( App\Http\Controllers\Local::class )
359 | );
360 | }
361 |
362 | return $classes;
363 | }
364 | ```
365 |
366 |
367 | ## Model Annotations
368 |
369 | You can use annotations to automatically bind your models to route parameters, using [Route Model Binding](http://laravel.com/docs/5.0/routing#route-model-binding). To do this, use the `@Bind` annotation.
370 |
371 | ```php
372 | /**
373 | * @Bind("users")
374 | */
375 | class User extends Eloquent {
376 | //
377 | }
378 | ```
379 |
380 | This is the equivalent of calling `Route::model('users', 'App\Users')`.
381 |
382 |
383 | ## Custom Annotations
384 |
385 | If you want to register your own annotations, create a namespace containing subclasses of `Collective\Annotations\Routing\Annotations\Annotations\Annotation` - let's say `App\Http\Annotations`.
386 |
387 | Then, in your annotations service provider, override the `addRoutingAnnotations( RouteScanner $scanner )` method, and register your routing annotations namespace:
388 |
389 | ```php
390 | addAnnotationNamespace( 'App\Http\Annotations' );
404 | }
405 | ```
406 |
407 | Your annotation classes must must have the `@Annotation` class annotation (see the following example).
408 |
409 | There is an equivalent method for event annotations: `addEventAnnotations( EventScanner $scanner )`.
410 |
411 | ### Custom Annotation Example
412 |
413 | Here is an example to make an `@Auth` annotation. It provides the same functionality as using the annotation `@Middleware("auth")`.
414 |
415 | In a namespace - in this example, `App\Annotations`:
416 |
417 | ```php
418 | hasPaths())
435 | {
436 | foreach ($endpoint->getPaths() as $path)
437 | {
438 | $path->middleware = array_merge($path->middleware, (array) 'auth');
439 | }
440 | }
441 | else
442 | {
443 | $endpoint->middleware = array_merge($endpoint->middleware, (array) 'auth');
444 | }
445 | }
446 |
447 | }
448 | ```
449 |
--------------------------------------------------------------------------------
/docs.md:
--------------------------------------------------------------------------------
1 | - [Annotations](/docs/master/annotations)
2 | - [HTML & Forms](/docs/master/html)
3 | - [Remote (SSH)](/docs/master/ssh)
4 |
--------------------------------------------------------------------------------
/html.md:
--------------------------------------------------------------------------------
1 | # Forms & HTML
2 |
3 | - [Installation](#installation)
4 | - [Important](#important)
5 | - [Opening A Form](#opening-a-form)
6 | - [CSRF Protection](#csrf-protection)
7 | - [Form Model Binding](#form-model-binding)
8 | - [Form Model Accessors](#form-model-accessors)
9 | - [Labels](#labels)
10 | - [Text, Text Area, Password & Hidden Fields](#text)
11 | - [Checkboxes and Radio Buttons](#checkboxes-and-radio-buttons)
12 | - [File Input](#file-input)
13 | - [Number Input](#number)
14 | - [Date Input](#date)
15 | - [Drop-Down Lists](#drop-down-lists)
16 | - [Buttons](#buttons)
17 | - [Custom Macros](#custom-macros)
18 | - [Custom Components](#custom-components)
19 | - [Generating URLs](#generating-urls)
20 |
21 | ## Replacement
22 | If you're looking to replace this package due to it's retirement we recommend using [Shift](https://laravelshift.com/convert-laravelcollective-html-to-spatie-laravel-html):
23 |
24 |
25 | ## Installation
26 |
27 | Begin by installing this package through Composer. Edit your project's `composer.json` file to require `laravelcollective/html`.
28 |
29 | ```bash
30 | $ composer require laravelcollective/html
31 | ```
32 |
33 | > Looking to install this package in Lumen? First of all, making this package compatible with Lumen will require some core changes to Lumen, which we believe would dampen the effectiveness of having Lumen in the first place. Secondly, it is our belief that if you need this package in your application, then you should be using Laravel anyway.
34 |
35 |
36 | ## Important
37 |
38 | Since this package internally uses strict comparisons (`===` instead of `==`) be careful when passing numeric values into your forms.
39 | Values in HTML are submitted as strings and Laravel old values stored in flash session are strings.
40 |
41 | In this example, this package will correctly insert `selected` HTML attribute into the radio input - because the passed value `'1'` strictly equals to the old submitted value in the session `'1'`:
42 | ```php
43 | echo Form::radio('category_id', '1'); // '1' === '1' - comparing passed value and old session value
44 | ```
45 |
46 | However, in this example, the passed integer value `1` is not strictly equal to the old submitted string value `'1'` in the session and the `selected` HTML attribute will not be inserted:
47 | ```php
48 | echo Form::radio('category_id', 1); // 1 === '1' - comparing passed value and old session value
49 | ```
50 |
51 |
52 | ## Opening A Form
53 |
54 | #### Opening A Form
55 |
56 | ```php
57 | {!! Form::open(['url' => 'foo/bar']) !!}
58 | //
59 | {!! Form::close() !!}
60 | ```
61 |
62 | By default, a `POST` method will be assumed; however, you are free to specify another method:
63 |
64 | ```php
65 | echo Form::open(['url' => 'foo/bar', 'method' => 'put'])
66 | ```
67 |
68 | > **Note:** Since HTML forms only support `POST` and `GET`, `PUT` and `DELETE` methods will be spoofed by automatically adding a `_method` hidden field to your form.
69 |
70 | You may also open forms that point to named routes or controller actions:
71 |
72 | ```php
73 | echo Form::open(['route' => 'route.name'])
74 |
75 | echo Form::open(['action' => 'Controller@method'])
76 | ```
77 |
78 | You may pass in route parameters as well:
79 |
80 | ```php
81 | echo Form::open(['route' => ['route.name', $user->id]])
82 |
83 | echo Form::open(['route' => ['route.name', 'id' => $user->id, 'foo' => 'bar']])
84 |
85 | echo Form::open(['action' => ['Controller@method', $user->id]])
86 |
87 | echo Form::open(['action' => ['Controller@method', 'id' => $user->id, 'foo' => 'bar']])
88 | ```
89 |
90 | If your form is going to accept file uploads, add a `files` option to your array:
91 |
92 | ```php
93 | echo Form::open(['url' => 'foo/bar', 'files' => true])
94 | ```
95 |
96 |
97 | ## CSRF Protection
98 |
99 | #### Adding The CSRF Token To A Form
100 |
101 | Laravel provides an easy method of protecting your application from cross-site request forgeries. First, a random token is placed in your user's session. If you use the `Form::open` method with `POST`, `PUT` or `DELETE` the CSRF token will be added to your forms as a hidden field automatically. Alternatively, if you wish to generate the HTML for the hidden CSRF field, you may use the `token` method:
102 |
103 | ```php
104 | echo Form::token();
105 | ```
106 |
107 | #### Attaching The CSRF Filter To A Route
108 |
109 | ```php
110 | Route::post('profile',
111 | [
112 | 'before' => 'csrf',
113 | function()
114 | {
115 | //
116 | }
117 | ]
118 | );
119 | ```
120 |
121 |
122 | ## Form Model Binding
123 |
124 | #### Opening A Model Form
125 |
126 | Often, you will want to populate a form based on the contents of a model. To do so, use the `Form::model` method:
127 |
128 | ```php
129 | echo Form::model($user, ['route' => ['user.update', $user->id]])
130 | ```
131 |
132 | Now, when you generate a form element, like a text input, the model's value matching the field's name will automatically be set as the field value. So, for example, for a text input named `email`, the user model's `email` attribute would be set as the value. However, there's more! If there is an item in the Session flash data matching the input name, that will take precedence over the model's value. So, the priority looks like this:
133 |
134 | 1. Session Flash Data (Old Input)
135 | 2. Explicitly Passed Value
136 | 3. Model Attribute Data
137 |
138 | This allows you to quickly build forms that not only bind to model values, but easily re-populate if there is a validation error on the server!
139 |
140 | > **Note:** When using `Form::model`, be sure to close your form with `Form::close`!
141 |
142 |
143 | #### Form Model Accessors
144 |
145 | Laravel's [Eloquent Accessor](http://laravel.com/docs/5.2/eloquent-mutators#accessors-and-mutators) allow you to manipulate a model attribute before returning it. This can be extremely useful for defining global date formats, for example. However, the date format used for display might not match the date format used for form elements. You can solve this by creating two separate accessors: a standard accessor, *and/or* a form accessor.
146 |
147 | To define a form accessor, create a `formFooAttribute` method on your model where `Foo` is the "camel" cased name of the column you wish to access. In this example, we'll define an accessor for the `date_of_birth` attribute. The accessor will automatically be called by the HTML Form Builder when attempting to pre-fill a form field when `Form::model()` is used.
148 |
149 | You must include the FormAccessible trait in your model definition for this to work.
150 |
151 | ```php
152 | format('m/d/Y');
173 | }
174 |
175 | /**
176 | * Get the user's date of birth for forms.
177 | *
178 | * @param string $value
179 | * @return string
180 | */
181 | public function formDateOfBirthAttribute($value)
182 | {
183 | return Carbon::parse($value)->format('Y-m-d');
184 | }
185 | }
186 | ```
187 |
188 |
189 | ## Labels
190 |
191 | #### Generating A Label Element
192 |
193 | ```php
194 | echo Form::label('email', 'E-Mail Address');
195 | ```
196 |
197 | #### Specifying Extra HTML Attributes
198 |
199 | ```php
200 | echo Form::label('email', 'E-Mail Address', ['class' => 'awesome']);
201 | ```
202 |
203 | > **Note:** After creating a label, any form element you create with a name matching the label name will automatically receive an ID matching the label name as well.
204 |
205 |
206 | ## Text, Text Area, Password & Hidden Fields
207 |
208 | #### Generating A Text Input
209 |
210 | ```php
211 | echo Form::text('username');
212 | ```
213 |
214 | #### Specifying A Default Value
215 |
216 | ```php
217 | echo Form::text('email', 'example@gmail.com');
218 | ```
219 |
220 | > **Note:** The *hidden* and *textarea* methods have the same signature as the *text* method.
221 |
222 | #### Generating A Password Input
223 |
224 | ```php
225 | echo Form::password('password', ['class' => 'awesome']);
226 | ```
227 |
228 | #### Generating Other Inputs
229 |
230 | ```php
231 | echo Form::email($name, $value = null, $attributes = []);
232 | echo Form::file($name, $attributes = []);
233 | ```
234 |
235 |
236 | ## Checkboxes and Radio Buttons
237 |
238 | #### Generating A Checkbox Or Radio Input
239 |
240 | ```php
241 | echo Form::checkbox('name', 'value');
242 |
243 | echo Form::radio('name', 'value');
244 | ```
245 |
246 | #### Generating A Checkbox Or Radio Input That Is Checked
247 |
248 | ```php
249 | echo Form::checkbox('name', 'value', true);
250 |
251 | echo Form::radio('name', 'value', true);
252 | ```
253 |
254 |
255 | ## Number
256 |
257 | #### Generating A Number Input
258 |
259 | ```php
260 | echo Form::number('name', 'value');
261 | ```
262 |
263 |
264 | ## Date
265 |
266 | #### Generating A Date Input
267 |
268 | ```php
269 | echo Form::date('name', \Carbon\Carbon::now());
270 | ```
271 |
272 |
273 | ## File Input
274 |
275 | #### Generating A File Input
276 |
277 | ```php
278 | echo Form::file('image');
279 | ```
280 |
281 | > **Note:** The form must have been opened with the `files` option set to `true`.
282 |
283 |
284 | ## Drop-Down Lists
285 |
286 | #### Generating A Drop-Down List
287 |
288 | ```php
289 | echo Form::select('size', ['L' => 'Large', 'S' => 'Small']);
290 | ```
291 |
292 | #### Generating A Drop-Down List With Selected Default
293 |
294 | ```php
295 | echo Form::select('size', ['L' => 'Large', 'S' => 'Small'], 'S');
296 | ```
297 |
298 | #### Generating a Drop-Down List With an Empty Placeholder
299 |
300 | This will create an `