List of important custom and in-built AMP tags

amp-img

amp-img is use as replacment of HTML img tag. amp-img tag effectively manage image resource at runtime. It uses lazy load to reduce page load time.

To use amp-img need to specify width and height, src attriburtes. It also support three optional attributes srcset, alt, attribution.

  • src – url of image need to display.
  • srcset – for retine ready images. We can provide different image for devices varying pixel ratio.
  • attribution – to set attribution text of the image.

To set image to responsive use layout attribute and set it value responsive.

Example of amp-img
<amp-img src="/img/amp.jpg"
srcset="/img/amp.jpg 1080w, /img/amp-900.jpg 900w, /img/amp-800.jpg 800w,
/img/amp-700.jpg 700w, /img/amp-600.jpg 600w, /img/amp-500.jpg 500w, /img/amp-400.jpg 400w,
/img/amp-300.jpg 300w, /img/amp-200.jpg 200w, /img/amp-100.jpg 100w"
width="1080"
height="610"
layout="responsive"
alt="an image"></amp-img>

amp-img is builtin tag and imported via the AMP js.

amp-video

amp-video is replacement of Html Video tag. It also lazy load video at runtime.

To use amp-video you must specify width, height and src attributes. It also support some more optional attributes poster, autoplay, controls, loop, muted. It also accept three unique HTML child nodes tag, placeholder before video start, and fallback.

Example of amp-video
<amp-video width=400 height=300 src="https://yourhost.com/videos/myvideo.mp4"
    poster="myvideo-poster.jpg">
  <div fallback>
    <p>Your browser doesn’t support HTML5 video</p>
  </div>
  <source type="video/mp4" src="foo.mp4">
  <source type="video/webm" src="foo.webm">
</amp-video>

amp-video is in-builtn tag and imported via the AMP js.

amp-audio

amp-audio is replacement of HTML audio tag. It is use to embed audio file in amp pages.

To use amp-audio you must specify src, width and height attributes, amp-audio also have three more optional attribute autoplay, loop and muted.

  • src – audio file url.
  • autoplay – It will autoplay animated image.
  • loop – it will auto loop audio.
  • muted – muted audio by default.
Example of amp-audio
 <amp-audio width="400" height="300" src="https://yourhost.com/audios/myaudio.mp3">
  <div fallback>
    <p>Your browser doesn’t support HTML5 audio</p>
  </div>
  <source type="audio/mpeg" src="foo.mp3">
  <source type="audio/ogg" src="foo.ogg">
</amp-audio>

To use amp-audio you have to include below script in <head> section

 <script async custom-element="amp-audio" src="https://cdn.ampproject.org/v0/amp-audio-0.1.js"></script>

amp-accordion

amp-accordion is use to implement accordion in amp pages. Accordion is mostly use in faq pages.

Amp-accordion can have one or more first child node <section> and each <section>contain 2 child node first one is heading and second node is content and clicking heading it will toggle display of content.

Below example of amp-accordion
 <amp-accordion>
  <section expanded>
    <h2>Section 1</h2>
    <p>Bunch of awesome content</p>
  </section>
  <section>
    <h2>Section 2</h2>
    <div>Bunch of awesome content</div>
  </section>
  <section>
    <h2>Section 3</h2>
    <amp-img src="/awesome.png" width="300" height="300"></amp-img>
  </section>
</amp-accordion>

amp-accordion also provides below attributes

  • disable-session-states – to disable preserving the collapsed/expanded state of the amp-accordion element
  • expanded – to set on any <section> expanded on page load.

To use amp-accordion you have to include below script in <head> section

<script async custom-element="amp-accordion" src="https://cdn.ampproject.org/v0/amp-accordion-0.1.js"></script>

amp-carousel

amp-carousel tag to display content boxes horizontally. A carousel is set of number of items with optional navigational arrow. The carousel move items if user swipe, or use arrow key or use navigation arrow.

To use amp-carousel you must specify height attribute, it also provide more optional attributes controls, type, loop, autoplay, delay.

  • controls – to display navigation arrow
  • type – by default its value is carousel all slides are shown and scrollable. If type value is Slides, it will display singe silde at a time. With type slides you can use three more attribute loop, autoplay, delay.
Example of carousel
 <amp-carousel width=300 height=400>
  <amp-img src="my-img1.png" width=300 height=400></amp-img>
  <amp-img src="my-img2.png" width=300 height=400></amp-img>
  <amp-img src="my-img3.png" width=300 height=400></amp-img>
</amp-carousel>

To use amp-audio you have to include below script in <head> section

<script async custom-element="amp-carousel" src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js"></script>

amp-lightbox

amp-lightbox tag to implement light box interface. amp-lightbox will have to define child node
to display child node content in lightbox on tap or click event.

Example of amp-lightbox
 <button on="tap:my-lightbox">Open lightbox</button>
<amp-lightbox id="my-lightbox" layout="nodisplay">
  <div class="lightbox">
    <amp-img src="my-full-image.jpg" width=300 height=800 on="tap:my-lightbox.close">
  </div>
</amp-lightbox>

To use amp-lightbox you have to include below script in <head> section

<script async custom-element="amp-lightbox" src="https://cdn.ampproject.org/v0/amp-lightbox-0.1.js"></script>

amp-ad/amp-embed

amp-ad is use to display ads. amp-embed is an alias of the amp-ad and use amp-embed when logically more accurate. Your ad javascript will not run inside your amp page, AMP will loads an iframe on runtime and execute ad javascript inside iframe sandbox.

To use amp-ad you need to specify type, width, height attributes. Type is name of ad network to which your ad belong. All data-* attributes also depend on type of network.

Below example amp-ad of for different type of network

 <amp-ad width=300 height=250
    type="a9"
    data-aax_size="300x250"
    data-aax_pubname="test123"
    data-aax_src="302">
</amp-ad>

<amp-ad width=320 height=140
    type="colombia"
    layout=responsive
    data-clmb_slot="129883"
    data-clmb_position="1"
    data-clmb_section="0">
</amp-ad>

<amp-embed width=400 height=300
    type="taboola"
    layout=responsive
    data-publisher=thepublisher
    data-mode=themode
    data-article=auto
    data-placement="Below Article Thumbnails">
</amp-embed>

To use amp-ad you have to include below script in <head> section

<script async custom-element="amp-ad" src="https://cdn.ampproject.org/v0/amp-ad-0.1.js"></script>

amp-analytics

amp-analytics is use to collect analytics data from an AMP page. Currently amp-analytics support below events:

  • Pageview
  • Anchor Clicks
  • Timer
  • Scrolling
  • AMP Carousel changes

To send data to analytcis server you need to specify json configuration object inside amp-analytics tag. Some vendor provide pre configure json configuration object. With type attribute you can set vendor and their json object.

Example for amp-analytics
<amp-analytics type=”abc”>
<script type="application/json">
{
 .........
}
</script>
</amp-analytics>

To use amp-analytics you have to include below script in <head> section

<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>

amp-iframe

amp-iframe to display an iframe in AMP pages. amp-iframe is more secure than html <iframe> tag.

To use amp-iframe you must follow these rules iframe must be at least 600px or 75% of the first viewport away from the top and can only request resources via HTTPS.

Example for amp-iframe
 <amp-iframe width="500"
      height="281"
      layout="responsive"
      sandbox="allow-scripts allow-same-origin allow-popups"
      allowfullscreen
      frameborder="0"
      src="https://player.vimeo.com/video/140261016">
  </amp-iframe>

To include an amp-iframe, first include the following script to the <head>

<script async custom-element="amp-iframe"
  src="https://cdn.ampproject.org/v0/amp-iframe-0.1.js"></script>

Amp also provide some more tags that we can use when require <amp-selector>, <amp-sidebar>,<amp-facebook>,<amp-instagram>,<amp-pinterest>,<amp-playbuzz>,<amp-twitter>,<amp-vine>,<amp-form>,<amp-font>,<amp-dynamic-css-classes>,<amp-anim>,<amp-app-banner> and more you can refer it on amp officia site.

Note: Also check AMP page example code

AMP (Accelerated Mobile Pages) Example Pages

AMP (Accelerated Mobile Pages) is a Google open source project to speed up page load in mobile devices.

AMP consist of three main parts

  • AMP HTML is HTML with custom AMP tags, properties and some restrictions like only subset of css and html are not allowed that slow down page loading. Only valid AMP HTML files will be shown in Google Search.
  • AMP JS library manage resource loading, provide custom tags, load external resources asynchronously so external resources should not block page rendering, pre-calculate layout of every element before any resources are loaded and it also disable slow CSS selectors.
  • Google AMP CACHE is a content delivery network. It fetches valid AMP HTML pages and cache them to improve page performance.

Sample AMP html

<!doctype html>
<html amp lang="en">
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<title>Hello, AMPs</title>
<link rel="canonical" href="http://example.ampproject.org/article-metadata.html" />
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "NewsArticle",
"headline": "Open-source framework for publishing content",
"datePublished": "2015-10-07T12:02:41Z",
"image": [
"logo.jpg"
]
}
</script>
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
</head>
<body>
<h1>Welcome to the mobile web</h1>
</body>
</html>

Required markup for AMP HTML document

  • Start your document with the doctype <!doctype html>.
  • Below doctype <html > tag (<html amp> is accepted as well).
  • Require <head> and <body> tags
  • First tag after <head> is <meta charset=”utf-8″>
  • Include a <script async src=”https://cdn.ampproject.org/v0.js”></script> tag as the second child of their <head> tag (this includes and loads the AMP JS library).
  • <link rel=”canonical” href=”$SOME_URL” /> tag inside their <head> tag that points to the regular HTML version of the AMP HTML document or to itself if no such HTML version exists.
  • Add a <meta name=”viewport” content=”width=device-width,minimum-scale=1″> tag inside their <head> tag. It’s also recommended to include initial-scale=1.
  • Add the following in <head> tag:
    <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>

Optional meta-data

A schema.org definition in above sample code is optional. However you can use it, if you want to distribute your content in some places like google search news top stories, rich cards, critic review cards according to your content type.

Validation

As we learn about required syntax need to follow , when you create AMP web page make sure that is valid AMP. It should display correctly to mobile devices to users. You can validate your AMP pages at https://search.google.com/search-console/amp.

Link AMP and non AMP pages

To make sure that search engines read (index) your we pages correctly. You need to add canonical link.

On normal web page –

<link rel="amphtml" href="amp-page-url" />

On AMP web page –

<link rel="canonical" href="none-amp-page-url" />

Demo

Non Amp page: https://sisnolabs.com/demo-non-amp.html
Amp Page: https://sisnolabs.com/demo-amp.html

Improved loading performance

Page load time, http request, page size all almost improved by more than 50%.

Note: In our next post we will going to write about AMP built-in and custom tags.

Upgrade Laravel 4.2 to 5.0 and above version

Laravel framework 5.1 and above versions support php7. Laravel 5.x has modified its application structure to make it more easy, robust and developer friendly. You can not directly upgrade Laravel 4.x to 5.1 or above versions. First you need to upgrade 4.x to 5.0 and then only you can upgrade Laravel to 5.1 or above version.

If you have application built in Lravel 4.x and you are looking to upgrade it to newest version of Laravel to get maximum advantages of this modern framework. Here are steps to upgrade.

  • Install Lravel 5.0 – Open terminal (for linux users), go to folder to where you want to install Laravel , then run below commandcomposer create-project laravel/laravel {directory} "~5.0.0" –prefer-dist

    or you can follow official installation documentation https://laravel.com/docs/5.0/installation

  • Remove vendor/compiled.php file
  • then run this command composer update from root folder of project
  • Open config/database.php and set correct database details
  • Move all files from old public folder (Lravel 4.x project files) to root folder of new installation (Lravel 5.0).
  • Correct path in index.php as below
    require __DIR__.'/bootstrap/autoload.php';
    $app = require_once __DIR__.'/bootstrap/app.php';
  • Correct path on bootstrap/autoload.php to
    require __DIR__.'/../vendor/autoload.php';
    $compiledPath = __DIR__.'/../vendor/compiled.php';
  • Copy and paste your old routes.php file into your new app/Http/routes.php.
  • Move all your controllers from app/Controllers into the app/Http/Controllers. add the app/Http/Controllers directory to the classmap directive of your composer.json file.
  • Create app/Models
    add “app/Models” to the classmap directive of your composer.json file.
  • Move your views from app/views to the new resources/views directory.
  • Move your language files from app/lang to the new resources/lang directory.
  • Move your tests from app/tests to the new tests directory.
  • Add “laravelcollective/html”: “~5.0” to your composer.json file’s require section.
  • Edit config/app.php and add this line to the ‘providers’ array:
    'Collective\Html\HtmlServiceProvider',
    for the 'aliases' array:
    'Form' => 'Collective\Html\FormFacade',
    'Html' => 'Collective\Html\HtmlFacade',
  • Pagination (changes in model/controller class files) Replace any calls to $paginator->links() with $paginator->render().

    Replace any calls to $paginator->getFrom() and $paginator->getTo() with $paginator->firstItem() and $paginator->lastItem() respectively.

    Remove the “get” prefix from calls to $paginator->getPerPage(), $paginator->getCurrentPage(), $paginator->getLastPage() and $paginator->getTotal() (e.g. $paginator->perPage()).

  • Run command at terminal
    composer update
  • Filters in route will not work. For that need to create Middleware class for after and before to work. Follow below tutorial to create middleware
    https://laravel.com/docs/master/middleware
  • We need to add below code on top of controller file, you will get coding error, below will help you to fix it.
    use DB
    So that we can use DB::table(‘Tablename’)

    For input data add on top of controller file
    use stdClass;
    use Input;

    to use model in controller add below code on the top of controller file:
    use App\Models\Category;

    To use segment() function
    Add Request $request in function parameter
    and replace
    Request::segment(4); with $request->segment(4);

    Replace View::make with view()->make

    Replace Config:: with config(‘constant.img’);

  • Admin authentication – changes in route .php
    in place of “before=>auth.admin” use “middleware=>auth.admin”
    and for middleware auth need to create Authmiddlewear class.

Now as we migrated project from laravel 4.x to 5.0 , It is ready to upgrade to 5.1 or above version.

To upgrade laravel 5.0 to 5.2

  • Update your composer.json file to point to laravel/framework 5.2.*.

    Add "illuminate/html": "5.2.*",
    "laravel/framework": "5.2.*",

    to require section of your composer.json file.

  • Add “symfony/dom-crawler”: “~3.0” and “symfony/css-selector”: “~3.0” to the require-dev section of your composer.json file.
  • Then in your config/app.php file, add this to your providers array:
    Collective\Html\HtmlServiceProvider::class
    and this to your aliases array:
    'Form' => Collective\Html\FormFacade::class,
    'Html' => Collective\Html\HtmlFacade::class,
  • Run composer update command at terminal

Thats it!

Update: also read Introduction to laravel 5.x application structure

Introduction to laravel 5.x application structure

There are more than 40 PHP frameworks available. The popular frameworks are Cakephp, Zend, Symfony, Codeigniter, Yii etc. In recent times Laravel got very popular among web developers. Currently, Laravel is one of the top PHP frameworks for modern web development.

popular php frameworks
popular php frameworks

Laravel did major changes in its Application structure from 4.x to 5.x.

Application Structure

Laravel 4.X
  • app
    • commands
    • config
    • controller
    • database
    • lang
    • start
    • storage
    • database
    • tests
    • views
    • filters.php
    • routes.php
  • bootstrap
    • autoload.php
    • path.php
    • start.php
  • public
    • css
    • fonts
    • img
    • js
    • locales
    • packages
    • .htacess
    • index.php
    • robots.txt
  • vendor
    • composer.json
    • composer.lock
    • server.php
Laravel 5.X
  • app
    • Console
    • Http
      • Controllers
      • Filters
      • Requests
    • Providers
  • bootstrap
  • config
  • database
    • migrations
    • seeds
  • public
  • resources
    • lang
    • views
  • storage
    • cache
    • logs
    • meta
    • sessions
    • views
    • work

Laravel 5.x Application structure

App directory only contain application core code. App directory contains directories such as console , http , providers.

  • The Console directory contains all of the custom Artisan commands for your application.
  • The Http directory conatins code to handle request entering your application.It conatins controllers, middleware, and form requests.
    – Filters for authentication in laravel 4.x have been converted to middleware , if you want to use filter you can still use in laravel 5.x.
    The old app/models directory has been removed but if you want to use it you can implement.

You can also create other directories and classes as per your requirement inside app directory to extend your application.

Config directory contains all configuration file of application.

Database directory contains your database migration and seeds.

Resources directory contains all your appliactions views , assest and language files.

Storage directory contains compiled blade templates, file based cache , logs etc.

Why Laravel 5.x changed Application structure?

New structure help developers to easily understand and work with framework , as you can see its follows “best practice” that development community follow.

It also implemented advanced objected oriented features like name spacing etc.

New route:cache Artisan command to speed up the registration of your application routes.

Most important key point is Laravel 5.1 and above version is compatible with PHP7.

Update: Also read Read how to upgrade Laravel 4.2 to 5.0 and above version