Wednesday, May 4, 2016

How to Use Ajax With OpenCart

Deploy Your PHP Application With Rocketeer

There used to be a time when PHP developers had to use deployment tools that were aimed at general web applications. You can see this in Johannes Schickling's tutorial on deploying Laravel applications with Capistrano, for example. It's a Ruby tool, and you need to write Ruby code. These tools get the job done but have limited integration with PHP applications. This can result in hacky solutions for certain scenarios. 

But nowadays, we're blessed with a few deployment tools written in our language that enable deeper integration. One of these tools is Rocketeer, a tool that takes inspiration from Capistrano and the Laravel framework.

Rocketeer is a modern tool that brings a great approach for your deployment needs. That is to run tasks and manage your application across different environments and servers. On top of that, it also has some magic in it, like installing Composer dependencies if it detects a composer.json file. You get sane defaults and automation of common tasks for a modern PHP application. And you have the ability to customise and extend everything.

You could describe it as an SSH task runner that runs client-side. The commands execute on servers through an SSH connection. If you use a shared hosting provider with only FTP access, you're out of luck, unfortunately. What you also need is a remote repository where the tool can fetch your code from. There is support for Git and SVN by default. Need support for another version control system? Write your own implementation with the provided interfaces.

Installation

You can install Rocketeer in two different ways. Either you download the phar file and make it executable or you install it through Composer. I'm a supporter of the latter. Having it as a dependency allows for easy installation when cloning the repository. This can benefit anyone cloning the repository to get them up and running.

Installing with Composer:

I don't recommend that you install it globally. Keeping it on the repository level will ensure that each person deploying is running the same version. What I do recommend is that you add vendor/bin to your PATH. Then you can use the binary by typing rocketeer in your project root.

Ignition

Let's get started! First you bootstrap the directories and files for configuration. You do this by running rocketeer ignite in the root of your project.

When your application ignites, the tool creates a .rocketeer folder in your project. The contents of the directory will look like this:

These are all the configuration files you need to start setting up your deployments. Whenever I refer to a configuration file from here on out, it exists in the .rocketeer/ directory.

Remote Folder Structure

It's important to understand how Rocketeer manages its folder structure on the server side, since it's a bit different to a regular setup. It uses a few directories for managing certain aspects of the deployment, so it can be effective at what it does. You specify a path to where you want to deploy your application on your server, and the tool will take care of the rest. That folder will look like this, if you have /var/www/app as your application directory.

The most important folder is current, which points to your latest release. That is where your web server's document root should be set to. So what happens when you deploy?

  1. The tool creates a time-stamped folder in the releases directory.
  2. Completes all tasks to make a ready release.
  3. Updates the symbolic link current to the new release.

This process makes your deployment transparent to the user. The switch between releases is almost instant, usually referred to as atomic deployments.

Some data should be persistent between your deployments. This can be file uploads, user sessions and logs, for example. Those files or folders go into the shared directory. The tool creates symbolic links inside each release for the ones you've configured.

Events

Events drive the tool, and all strategies and tasks fire a before and after event when they run. They also provide a special halt event when a task fails. This could be for example dependencies.halt, or deploy.halt for general failure. This allows us to hook into the process where we need to.

The default events that happen during a deployment are:

  • deploy.before: before anything happens.
  • create-release.before: before it creates a new release directory.
  • create-release.after: after creating a new release directory.
  • dependencies.before: before installing or updating dependencies.
  • dependencies.after: after installing or updating dependencies. Perhaps make sure that binaries in your vendor folder are executable.
  • test.before: before running tests.
  • test.after: after running tests.
  • migrate.before: before running database migrations. Maybe you want to do a backup of your database?
  • migrate.after: after running database migrations.
  • deploy.before-symlink: before symlinking the release as our current release.
  • deploy.after: completed. You could notify people that everything was smooth sailing or otherwise.

We can also create our own events that we can fire and listen to. For now, we'll stick with these events provided for us. They will be enough for us right now.

Tasks

At the heart of Rocketeer, we find a concept called tasks. Most of what is happening under the hood are core tasks. The definition of a task could be a set of instructions to perform as a step in a deployment. If we look at some of the classes that the tool provides, we can get a general feel of what tasks are: classes such as DeploySetupMigrateRollback, and Dependencies. When you deploy, the deploy command itself is a task with sub-tasks.

Types of Tasks

This is where you'll start to see how integrated the tool is with PHP, since you'll write tasks in the language. You can create your own tasks in three different ways:

Arbitrary terminal commands. These are one-liners that you want to run on your server. Can be useful for a lot of things, like running gulp build  ---production for example.

Closures. If you need a bit more flexibility or complexity, you can write a task as a closure (anonymous function). Say you'd like to generate documentation for an API during your deployment.

Classes. For more complex tasks, you should utilise the option to create classes for tasks. You create a class and extend Rocketeer\Abstracts\AbstractTask. Then you must provide at least a description and an execute() method. Here's a completely useless example just to show the structure of a task class:

Note that you have to register task classes yourself. Either you do this through the hooks.php file and add it to the custom array...

... or you can do this through the facade:

Once you register it, you can execute it:

Defining Tasks

We discussed events first because we hook tasks in where we need them in the process. You can do this in a few ways. Go with the one you like and that meets requirements for your level of complexity.

The easiest way of defining your tasks is in the hooks.php file. It provides two arrays for this, specifying task execution before or after certain events.

Strategies

You might be able to tell already that the tasks provided are quite generic. Take Dependencies, for example. What kind of dependencies are we talking about and which package manager? 

This is where strategies come into play. A strategy is a specific implementation of a task, such as running tests with Behat or using Gulp for building your front-end. Tasks have a default strategy with the option of running the other strategies through the CLI. We can list the strategies available like this:

Creating Your Own Strategies

Say you're doing BDD With Behat for your application instead of TDD. Then you want to run your tests with Behat instead of PHPUnit. Since it is a test runner, there is already a strategy namespace for that, but no implementation. Create the directory .rocketeer/strategies/ and place your new BehatStrategy.php in there.

You can now switch out your test strategy to the new implementation in strategies.php.

Connections & Stages

It doesn't matter if you have an infrastructure in place or have one in mind. It doesn't matter if your application deploys to many environments across many servers. Rocketeer will be there for you. You can even have many varying locations on the same server. This is where the terms connections and stages enter.

connection is a server where you deploy your application to. This is often called an environment, and production and staging are examples of this. Configuring these connections is a breeze in the tool. Either you do it through a nested array or by keeping separate files for each connection. Each connection can also have multiple servers in it.

Stages are like connections inside connections, a kind of "connectionception". You could set up a staging and a production environment on a single server with the use of stages. So instead of having two separate connections, you have one connection with two stages in it.

Plugins

A great feature is that we can extend our process using plugins. There are a few official ones for integration with LaravelSlackHipChat and Campfire. Then there are a few, but not that many, on Packagist. Installing plugins is an easy task through the CLI:

Even though there's a limited number of plugins, it leaves room for developing plugins in the future. It tells of a good philosophy. And why not develop one of your own?

Setting Up Your Deployment

To get your application off of the ground, you need some basic configuration. You need to tell Rocketeer where to find your application and where it should deploy it to. Let's start by setting an application name and configuring a production server in config.php.

You now have an application name and a server to deploy your application to. This setup uses SSH key authentication, but you can connect with a username and password also. To get prompted for username and password, set 'key' => ''. The tool will store the credentials on your local machine and use them each time later on. I don't recommend setting a username and a password in the config file, because you never want credentials committed to your repository.

What you should now do is change the default connection that you deploy to. Having the default set to production is not ideal. You don't want to deploy to production by accident. So in the same file, look for the default key and change the value to staging instead.

The application name itself isn't that important. But if you don't specify a folder to deploy to, it will use this as the folder name in the root directory. By default, the root is set to /home/www. With this application name, it will deploy it to /home/www/my-deployable-app. If you want to change your root directory, you can change this in remote.php.

In the same file, you have the ability to override the application name and specify a directory for your application.

Now you have a receiving end of the deployment, but you also need to set up the location of your code so it can be fetched. You do this by setting up your remote repository in scm.php. By default it uses Git, but it has support for SVN also. You tell it the address of our repository and supply credentials if needed. I suggest you use SSH key authentication here as well, and leave the username and password empty.

Since you added a connection to the production server, you want to deploy the master branch.

Connection & Stages Specific Configuration

In most cases, you don't want the same configuration option for all your connections or stages. Say, for example, you want to deploy another branch to the staging environment. Rocketeer allows you to override configuration values for connections and stages using config.php. To deploy a branch called staging on your staging connection, you do this:

It uses a nested array to override configuration values. Under the staging key, find the corresponding key in the file you want to change. In this case it's branch in scm.php.

Deploy, Lift Off!

Now you have everything set up to make a successful deployment. You haven't met your requirements for a complete deployment, but it's enough to get your application cloned to your server and served to the end users. First you can execute the check strategy to see if your server meets the requirements.

If everything is okay, you can deploy by running:

Since this was your first deployment, Rocketeer will make sure everything is up to par. The tool creates the directories it needs and that our application will live in. If everything is smooth sailing, you should have a complete build of your application on the server.

If you changed the default connection to staging in the previous section, it will always deploy to staging. That is, of course, unless you tell it to deploy to somewhere else. When you want to deploy on a different connection or more than one, you use the --on switch.

Want to have a look at what will happen on your server once you hit the button? Use the --pretend flag to let the tool tell you what it will execute on the server.

Houston, We Got a Problem. We Need a Rollback.

Unfortunately we need to take care of deployments that break functionality or wreak havoc in the infrastructure.  Then you need to make a quick rollback to your latest release. Luckily it's a simple operation—just run the command:

Since it stores a number of builds, performing a rollback is fast. It changes the symbolic link of current to the previous release.

Shared Directories

Setting up shared directories is simple—just add them to the shared array found in remote.php. Rocketeer will create and link these folders for you in the deployments after. The specified paths should be relative to your root folder.

Writable Directories

Most shared directories will also need the web server to be able to write to them. Writing logs, sessions or file uploads is often a task performed by any application. These you add to the permissions.files array in remote.php.

Install or Update Dependencies

Installing or updating dependencies is something you need if the application relies on any kind of dependencies. The tool comes with support for the most popular package managers. Configuring anything is not necessary if you have the default setup for them. It will detect and install or update dependencies for ComposerNpmBower and Bundler. The default strategy for dependencies is set to Polyglot. This is the tool's way of detecting and installing dependencies for the different package managers.

But let's say that you want to install all dependencies on staging, and the tool uses the --no-dev flag by default. Perhaps you want to install PHPUnit for running tests, which is a development dependency. In strategies.php, you can find the composer key, which tells the tool how to execute Composer. You can then override this in config.php:

Database Migrations

Migrating databases is often something you want to do when you have a complete release, just before it symlinks to current. Whatever tool you use, you can tell it to run before the deploy.before-symlink. This hook is not a regular one, but an internal hook. You then need to register it someplace else than hooks.php. You can this do this in events.php, which you can create if it doesn't exist already.

Running Tests

Running tests in a deployment process is a great way of ensuring that no broken code or tests slip through the cracks. By default, the tool uses PHPUnit, and you can hook the test runner to run after dependencies are installed or updated.

We should now see it's executing PHPUnit on each deployment, and in case of any failing tests it will abort. Make sure you see output from it, otherwise it might have a problem with finding a PHPUnit binary or your test suite.

Front-End Build Tools

Often our applications are not only a back end, unless they are a REST API for example. Running build tools for our front end is a common task with tools such as GruntGulp or Webpack. Making this part of our deployment process is nothing fancier than using a hook to run the commands such as:

A front end often relies on dependencies as well, so run tools after installing or updating them.

Tips & Tricks

Running Updates

If you do not want to create a new release when you deploy, you have the option of running an update. Be cautious when doing this since you won't be able to roll back to the previous version, only the previous release. But it is a quick and simple way of updating your application with the latest changes with:

Local Tasks

Sometimes it can be nice to run tasks in your local environment. Say you want to run a PHPCS check or build your static assets and upload them to the server, removing the need of certain binaries on the server. If you create a task class, you can set the protected variable $local to true.

Conclusion

The deployment process is an important part of an application's lifecycle. Tools like Rocketeer allow you with ease to make this an uncomplicated matter. This is especially true when using it for a PHP application since it integrates so well with it.

Writing an introductory tutorial to Rocketeer turned out to be a hard task. The tool is so flexible that drawing the lines on where to stop isn't easy. I hope I got across the possibilities in using this tool and how it can benefit you and your application. If you want to dig deeper, I suggest reading the full documentation. There's much more to the tool than what I could cover in this article.


by Niklas Modess via Envato Tuts+ Code

This Week in Mobile Web Development (#106)

Read this on the Web

Mobile Web Weekly May 4, 2016   #106
Brian Rinaldi recommends
Making Sense of The Mobile Development Ecosystem — The first in a series of posts on how to decide which mobile development technology would work best for your project focuses on the mobile web.
Brian Rinaldi
Holly Schinsky recommends
Ionic Framework: A Definitive 10,000 Word Guide — A comprehensive guide to creating a hybrid app with Ionic from development to publishing in the app store. Useful info for all hybrid developers.
Nikola Brežnjak
Holly Schinsky recommends
Creating Cross-Platform Apps with Angular 2 [video] — A 20 minute talk from Brad Green of the Angular team.
Channel 9
Sponsored
FREE Trial, 125+ Mobile Web Development Courses with Stone River eLearning. — Join 300,000 students developing their skills online. This free trial includes full access to our catalog of 125+ technology courses - over 500 hours of video content focused on development and design.
Stone River eLearning FREE TRIAL

Chris Brandrick recommends
Twitter for Mobile Gets React Powered Overhaul — Twitter gets a new React-powered experience for its mobile Web site.
David Bellona
Brian Rinaldi recommends
An Interesting Change to Browsing On Android — Search results from the Google Android app now open in a Chrome custom tab. Peter Gasston says this may have an impact on mobile browsing habits.
Peter Gasston
Chris Brandrick recommends
Spotify Removes The Hamburger Button on iOS — Nav bars are in, hamburgers are out.
Jacob Kastrenakes
Brian Rinaldi recommends
Bots Won't Replace Apps. Better Apps Will Replace Apps. — Why conversational UI fails in practice and what the West can really learn from Asian messaging apps.
Dan Grover
Holly Schinsky recommends
A Baseline for Hybrid Mobile Developers — Ray Camden discusses the base set of skills required for developers wanting to pursue hybrid mobile app development.
The Official Ionic Blog
Brian Rinaldi recommends
Making IFrames Responsive — Because the page does not have access to the content within the iFrame, this workaround uses the postMessage API to communicate.
Ire Aderinokun
Holly Schinsky recommends
Onsen UI 2 - Next-Gen Hybrid Mobile App UI Framework now with React Components — Onsen UI Mobile App Framework releases v2 and includes React Components as well as support for both iOS and Android via Material and Flat design.
Onsen UI
Sponsored
Try RASON™ – RESTful Analytic Solver™ Object Notation for Web/Mobile Apps — Solve powerful optimization & simulation models in your app easily with RASON®. Use a high-level modeling language embedded in JSON, & a simple, Azure-backed REST API.
Frontline Systems Inc

Holly Schinsky recommends
Integrating Firebase with AngularFire2 & Ionic 2 — A 2 part series on building an app that can authenticate, create accounts and more using Firebase with AngularFire2 and Ionic 2.
Clearly Innovative
Brian Rinaldi recommends
Creating An Ionic Theme for NativeScript — Rob Lauer shares his theme for creating native mobile apps using NativeScript that have the visual appeal that the Ionic framework is known for.
Telerik Developer Network
Brian Rinaldi recommends
My Experience Developing with Telerik NativeScript — Nic Raboy shares his opinions of Telerik NativeScript and experiences using it to build native mobile apps with JavaScript.
The Polyglot Developer
Holly Schinsky recommends
A Sample App using Phonegap/Cordova Plugins with ReactNative — Sample kitchen sink app showing the use of Cordova plugins with ReactNative.
Parashuram's blog
Brian Rinaldi recommends
Ionic Raises $8.5m — Ionic (formerly Drifty), the company behind the hybrid mobile framework of the same name, announced a major round of funding.
Jordan Novet
Job listing
Hired is the best place to find engineering jobs. — Bored at work? Want to try a new stack? Find 3500+ great tech companies on Hired who will compete to hire you. Try it today.
Hired.com

Curated by Brian Rinaldi and Holly Schinsky for Cooper Press.
Cooper Press is located at Office 30, Fairfield Enterprise Centre, Louth, LN11 0LS, UK
Update your email address
or stop receiving MWW here


by via Mobile Web Weekly

New Facebook Sharing Options: What Marketers Need to Know

kh-facebook-sharing-560

Do you want more people to share your content on Facebook? Are you taking advantage of Facebook’s sharing features? Facebook offers a variety of new buttons and plugins that make it easier than ever for readers to share and engage with your content on Facebook. In this article you’ll discover how the new social sharing [...]

This post New Facebook Sharing Options: What Marketers Need to Know first appeared on .
- Your Guide to the Social Media Jungle


by Kristi Hines via

Product Showcase : jQuery Slider with Icons

A jQuery plugin to showcase your top products on your webpage. This plugin will create a simple image slider with icon controls and overlay text.

The post Product Showcase : jQuery Slider with Icons appeared first on jQuery Rain.


by Admin via jQuery Rain

jPop : jQuery Popup plugin

jPop is Popup jQuery plugin that helps you to make awesome popup. jPop renders when a visitor scrolls down.
After visitor scrolls down to 200px (As a default but you can change it). You can change its title,paragraph,Button text and class and input through initializing plugin.

The post jPop : jQuery Popup plugin appeared first on jQuery Rain.


by Admin via jQuery Rain

Rumchata

The official drink of paradise
by via Awwwards - Sites of the day