Laravel

How to use HTML form elements in laravel ?

Here, i have shared how to implement Laravel’s HTML facade.

> Converting a HTML String to Entities

You want to “escape” html in your web page output.

You know you can use the PHP htmlentities() method, but want to do it the Laravel way.

Use the HTML::entities() method.

The above will convert the less than symbols to < and the greater than symbols to >

You can also use the helper function e().

The above will produce the same output as the longer HTML::entities() method.

>Decoding HTML Entities to a String

You have a string encoded by HTML::entities() and want to decode it back to the original string.

Use the HTML::decode() method.

This is the opposite of HTML::entities(). For example.

In this example $string will equal “<h1>Hello</h1>”;

> Generating a Link to a Javascript File

Use the HTML::script() method.

Just pass the path to the javascript file.

This produces the following HTML code.

If the file path you pass isn’t a complete URL, Laravel will use your application’s URL to build a complete URL.

You can pass additional attributes in an array as the second argument.

The attributes will be added to the script tag as the result below illustrates.

> Generating a Link to a CSS File

Use the HTML::style() method.

You can just pass the path to the style sheet.

This will add the following HTML.

If the file path you pass isn’t a complete URL, Laravel will use your application’s URL to build a complete URL.

You can pass additional attributes in an array as the second argument. This also allows you to override the default attributes

Now the media is “print” instead of the default “all”.

> Generating an HTML Image Element

Use the HTML::image() method.

The only required argument is the path to the image.

Which produces the following HTML.

If the image path you pass isn’t a complete URL, Laravel will use your application’s URL to build a complete URL.

You can add the alt attribute with the second argument.

Now the HTML will look like the following.

The third argument, if used, must be an array. It contains additional attributes to add to the img tag.

Now the HTML contains a class attribute.

> Generating a HTML Link

Use the HTML::link() method.

If you only pass the URL as the first argument it will create a link using the URL as the title.

This produces the following HTML.

You can add a second argument for the title.

Which will produce the following.

The third argument, if used, must be an array. It allows you to assign other attributes to the tag.

Now the anchor tag also has the id attribute. Notice also that the title is back to the URL because we passed null as the second argument.

A fourth option can be passed to make the URL secure. Note that this only works when the URL is automatically built (see the discussion below).

This produces something like what’s below.

> Generating a Secure HTML Link

Use the HTML::secureLink() method.

This only works correctly when you’re building URLs for your application. In other words, if you pass a fully formed URL to the method, it will use the fully formed URL.



You pass paths to this method, not URLs.

If you only pass the path as the first argument it will create a link using the full URL as the title.

This produces the following HTML.

You can specify the title as the second argument.

The resulting HTML is below.

You can also pass an array of attributes as the third argument.

Now the anchor tag has a class attribute.

> Generating a HTML Link to a Named Route

Use the HTML::linkRoute() method.

The only required argument is the first one, the name of the route.

Depending on your app/routes.php file, this may output something like.

You can pass a second argument to specify the title to display.

If you’re route takes parameters, then you must pass a third argument.

The output could look something like below.

You can specify an array as the fourth parameter. This array should contain any additional attributes to apply to the anchor tag.

Now the anchor tag has a class attribute.

> Generating a HTML Link to a Controller Action

Use the HTML::linkAction() method.

Note that the controller and action you specify must exist AND there must be a reference to it somewhere in your app/routes.php file.

The simplest form is a single argument, the controller@action.

This could produce something like.

You can specify the title as the second argument.

Which produces the following.

And, if additional attributes are needed use the fourth parameter.

Now the anchor has a class attribute.

  > Generating a HTML Link to an Email Address

Use the HTML::mailto() method.

The first argument to the method is the email address.

This will create the mailto link and display the email address as the link text. Notice that Laravel automatically and randomly obfuscates the address. But the address will appear correctly in browsers.

(Note backslash above is used to continue line for smaller screens.)

If you pass a second argument, that becomes the link text.

That will produce something similar to below (the actual obfuscation will vary).

(Backslash above is used to continue the line for smaller screens.)

You can pass a third argument as an array of attributes to apply to the anchor tag.

Now the anchor has a class attribute.

> Generating an Ordered List of Items

Use the HTML::ol() method.

You simply pass the method a list of items. If you pass an associative array, only the array values are used.

This produces a simple list.

If any of the elements are arrays, then a sub-list is generated.

If the Blade template has the following:

Then the following would be output.

If you want the sub-list to have a title, then have the array representing the sub-list have a key.

If the Blade template has the following:

Then the following would be output.

If you want attributes to apply to the list, use an array as the second argument to HTML::ol().

Now the list has a class attribute.

 For Unorder List you can use <ul> </ul> instead of <ol> </ol>