Month: May 2013

Live Text Editing on a Remote Server using Rmate and Rsub

What is a remote server?

A remote server is a repository for your applications and files that is not connected to your local network.

Remote servers can be:

  • Database servers
  • Web servers
  • DNS (Domain Name Service) servers
  • Mail servers
  • Proxy servers
  • Application servers, and
  • Real-time Communication Servers (skype, VoIP)

How do you connect to and communicate with a Remote Server?

There are multiple ways:

  • FTP: File Transport Protocol, using a file transfer program such as FileZilla or WinSCP. You just need to setup the hostname (your server address) and enter your authentication details (username and password) and you will be able to transfer files from your local pc to the remote server.
  • Telnet: Is a protocol that allows for bidirectional (to and from server) text communication. Similar to command line and terminal to communicate with the remote host. Unfortunately Telnet does not encrypt data sent to and from the server. There is also no default authentication so this protocol has been rarely used based on it’s security flaws.
  • Secure Shell (SSH): Is a cryptographic network protocol that secures data communication, remote shell services and command line execution between computer. Common applications for windows is Putty but it is built in with Unix like operating systems as (Terminal).

Common Server Commands

Change Directory

cd foldername   //move into that directory
cd /foldername  //move into that directory which is one up from where we are
cd ..           //move to the directory one up from where we are

Show Directories

ls       //show directories and files
ls -la   //show directories and files with additional information

Editing A File

vim filename.txt       //edit a file with the in-built vim editor

There is however a limitation of vim:
Because it is run through command line, functionality such as: find and replace, use of cursor, copy, paste and other common features in text editors are missing.

So How can we Edit Text Live on the Server?

Well the Solution is:

Rmate or Rsub

1. Install SublimeText or Textmate on your pc

(Sublimetext is for all OS’s, Textmate only Mac)

2. SSH into your server

3. Install Ruby Gems on the Server

4. install rmate

gem install rmate

5. update rmate

gem update rmate

6. tell sublimetext or textmate to listen on port 52698
7. use the following command to open the file in sublimetext or textmate from the server

ssh -R 52698:

rmate filename.txt

8. you can now edit the file as you would a in a normal textfile, when you hit save it will save on the server.

What is MVC?

In the web development realm it is very common we hear the term MVC. Well What is MVC?

MVC is a software design approach. MVC stands for:

  • Model
  • View
  • Controller

It is a template or pattern on how to structure a program. MVC has its origin in the Smalltalk language, MVC has been widely adopted by many languages and particularly by frameworks.

An MVC example using java applet


What is MVC?

The basic concept of MVC is simple to understand but the actual implementation can be tricky. It takes some time to master where you put your code.

MVC separates three core pieces: the data, the actions a user takes and the visual display or user interface. By adopting MVC you create a more modular, easily maintainable and readily expandable project.

  • Model: Combination of data used by the application and the business rules that apply to the data
  • View: The interface through which the user interacts with the application.
  • Controller: The agent that responds to user actions, makes use of models and is the place where things are done.

A project of application will almost always have multiple models, views and controllers.

If you think of MVC like a pyramid the model will be on the botton, interacting with the database, the controller in the middle interfacing between the model and the top layer the view or presentation layer.

Actual code should be distributed with most of it in the model, some in the controller and very little in the view. HTML should however all be in the view files.

What is MVC: Models

Models are often tied to database tables: where one instance of a model represents one row of data from one table. You want to keep your models as singular as possible: ie. Page, User and Comment. Another area where a model should be used is in storing information about a contact form, it does not interact with the database but is still the correct location for that specification.

Models are containers for data but they also dictate the rules for the data (validation).

What is MVC?: Views

Views are pretty staight forward, views contain HTML and reflect what the user will see and interact with. You might have the following view files:

  • Primary Layout of Site
  • Display of single page of content
  • Form for adding or updating a page of content
  • Listing of all the pages of content
  • Login form for users
  • Form for adding a comment
  • Display of a comment

Views must contain some PHP that adds unique content for a given page. The most logic a view should have, however, is conditional to confirm that a variable has a value.

Decoupling or separating data from the presentation of such data is useful because it allows the easy change of view ata without sifting through PHP or ASP code. It also allows you to use the same data in many different outputs.

What is MVC?: Controllers

The controller acts as the glue between the model and the view. The controller represents actions. Normally the controller defines responses to user events: the submission of a form and the request of a page. A guiding principle is “Fat Model, Thin Controller”. This means you should keep pushing your code to the foundation of the application (the model), as the model is more reusable than the controller.

Remember: Fat Model, Then Controller

To put it in context:

A user would go to:

This is a request for the site to show page with id of 1.

The request is handled by the controller, it would validate the provided id, load the associated data as a model instance and pass he data onto the view.The view would then insert the data into the right place in the HTML template, completing the user interface.

Source: Larry Ullman, Yii Book

please support Larry and buy the book it is really good





PHP & OOP: A Fundemental Tutorial on Object Oriented Programming and PHP

PHP & Object Oriented Programming

PHP can be used both procedurally and with an object oriented approach.

In PHP & procedural programming the emphasis lies with the actions and the steps that must be taken to perform actions such as submitting a form or retrieving a record from a database.

In PHP & Object Oriented Programming the emphasis lies on the objects and the analysis and definition of the kind of things each object will have (attributes) and do (functions and procedures).

php &


PHP & the Class

The core concept behind PHP & OOP is the class. A class is the blueprint or template defining an object: the information is needs to have and the common actions it does. To represent an HTML page as a class: attributes would be title, content, date created and date last updated. Actions would be remove HTML tags or show Preview.

With that in mind the HTMLpage class would have attributes [title, content, datecreated, datemodified] and methods [stripHTML(), showPreview()].

PHP & the Object

Once you have created the class blueprint, we can now create objects baed on the class. So Home page an object representing an HTMLpage, About Us as object representing an HTMLpage and Cool Stuff an object respresenting an HTMLpage. Home Page, About Us and Cool Stuff would all have the same attributes and methods within themselves (objects). However, the content within each page would be different ie. about us would have about us content and home page would have homepage content.

A good class makes projects more reliable and easier to maintain. A proper PHP & OOP philosophy requires a good amount of theory and design as opposed to a procedural which writes itself as a logical flow.

Bad PHP & procedural programming tends not to work well but can be easily treated, bad PHP & OOP programming is a complicated mess which can be a real chore to fix. Good PHP & OOP is easy to extend and reuse.

PHP & Concepts

PHP & Modularity

Modularity is breaking function down into more manageable bite sized chunks.

PHP & Encapsulation

Encapsulation means how something works and how it is used is separated. For example stripping a page of HTML, you would not need to know how a method does that, as all you need to do is use it.

This concept goes hand in hand with access control (visibility). Proper access control improves an application’s security and reduces the risk of bugs. There are 3 levels: Public, protected and private.

PHP & Inheritance

One class can be defined as an extension of another, setting up a parent-child relationship (base-sub class). The child class can inherit members of the parent class. A public member can be accessed anywhere within the class, derived class or through object instances of those classes. A member defined as protected can only be accessed within the class or within derived classes but not through object instances. A attribute marked private can only be accessed within the class itself, not derived classes.

PHP & Abstraction

Ideally base (parent) classes should be as generic as possible, specific functionality should come when child (sub classes) extend from them. Inherited classes will inherit all public and protected members and can define its own. eg. A person class has eat() and sleep() functions. An adult inherited class will have eat() and sleep() and work() as an added abstracted member method.

Note: PHP does not allow a single child class to inherit from multiple parents.

PHP & Polymorphism

Child classes can also override a parent class’s method. It does this by redefining what that method does, this is known as polymorphism.

PHP & Syntax


class ClassName

Class with attribute and function (with visibility indicators):

class ClassName
public $variable;
public function thisFunctionName(){
//function internals

Note: public is the default visibility, but it is best practise to be explicit.

Calling a class function:


Creating an instance of Class (object):

$object = new ClassName();

Referencing public attributes and methods:

echo $object->variable;

Referencing attributes and methods within the class:

Class ClassName
public $variable;
public function thisFunctionName(){
$this->variable = 50;
return $this->variable;

Constructors and Destructors:

function _construct

function _destruct()

A constructor often takes arguments (parameters) but cannot return any values. A destructor cannot take arguments.


class ChildClass extends ParentClass

Inheritance is indicated using the extend keyword.

PHP & Naming Conventions

The best practices for naming is as follows:

  • ClassName: Upper-CamelCase (ClassName, ChildName)
  • methods and attributes: lower-CamelCase (doThis() or someVar, fullName)
  • Private Attributes: underscore in front (_variable)

Source: Larry Ullman, Yii Book Introduction

Please Support him and buy the book if you are interested in the Yii Framework