Kohana

KOHANA

Kohana is a PHP framework that follows MVC architecture (Recently it’s based on HMVC Architecture).

What is MVC ARchitecture?

M-Model–>objects–these objects encapsulate data–dbms

V-View–>presentation–what the user sees–css, html, javascript.

C-Controller–>link between model and view–respond to events by the user (user-interaction) and invoke changes in the model and view based on that.This consists of routers which map certain piece of code to certain url.

In basic web architecture, browser interacts pages and these pages might contain lump sum amount of code on doing something and giving the output for us and even fetching things from database. So , instead of having one-page with all the code it in we can organize this in a better way by breaking into parts which can be done by using MVC architecture ..tadaaaaaa!!!!

So, now our browser interacts with the controller and controller decides on what to do based on the request sent by the browser. If any data has to be fetched or anything has to be done on data then the controller interacts with the model and all the code for getting the data or manipulation of data or connecting to the database is all put in the model. Once the model’s done with its work it sends back the data to the controller and the the controller sends it back to the View and the view returns what kind of html, css , javascript has to be returned to the browser.So the server puts together all the content (data) and formats it into a template and sends it to the user.

MVC is popular as it isolates the application logic from the user interface layer and supports separation of concerns.200px-mvc-process-svg

Kohana installation on Ubuntu 15.04:

Terminal:

To download kohana:

wget https://github.com/kohana/kohana/releases/download/v3.3.6/kohana-v3.3.6.zip

Unzip:

aptitude install -y unzip

unzip kohana-v3.3.6.zip

Create a new directory(MyDemo):

mkdir /var/www/html/MyDemo

Copy the contents in kohana to MyDemo:

cp -r kohana-v3.3.6/* /var/www/html/MyDemo

The installation’s subdirs may have lost their permissions during zip extraction so Chmod them all to 755 by running :

find . -type d -exec chmod 755 {} \;

http://www.computerhope.com/unix/uchmod.htm (on chmod)

Move to MyDemo dir:

cd /var/www/html/MyDemo/

Set permissions

find . -type d -exec chmod 755 {} \;

After installing Kohana, you need to setup virtual host and add it to DNS:

sudo a2enmod rewrite a2enmod headers

Setting up virtual host:

sudo gedit /etc/apache2/sites-available/000-default.conf
Here, Enter this before vm and save it:

<VirtualHost *:80>
ServerAdmin mydemo.com
DocumentRoot “/var/www/html/MyDemo/”
<Directory “/var/www/html/MyDemo/”>
Options Indexes FollowSymLinks Includes execCGI
AllowOverride All
Require all granted
</Directory>
ServerName mydemo.com
ErrorLog “/var/log/apache2/error.log”
</VirtualHost>

Add this virtual host to DNS:

sudo nano /etc/hosts

Add this:

127.0.0.1 mydemo

and save it.

Now run your server:

sudo service apache2 restart

When you do the environment test, you might get some permission error in cache and log directory. For this:

(assuming you are in MyDemo Directory)

chmod a+x application/logs/ application/cache/

(or)

chmod a+w application/logs/ application/cache/

Now remove or move install.php

In application/bootstrap.php

change the time zone and go to

Kohana::init(array(
‘base_url’ => ‘/kohana/’,
));

and change this to:

Kohana::init(array(
‘base_url’ => ‘/MyDemo/’,
));

So, by default it goes to the Welocome controller and to the page index.

This means there is a class named Welcome with function index.

mydemo.com/index.php/welcome

You can take parameters too:

You can see in your bootstrap.php that (<controller>(/<action>(/<id>)))’) This means that anything after action is a parameter which is named as id

Lets write a route to fetch these parameters:

public function action_fetchid(){

$a=$this->request->param(‘id’);

$this->response->body(“You said : ” . $a);

}

Now in your browser go to http://mydemo.com/index.php/welcome/echo/ImGood

This gives o/p:->You said : ImGood

Look at https://github.com/shreyavshetty/BloggingSite

How web works?

Clients and servers:

Computers connected to the Web are called clients and servers.

Clients are the typical Web user’s Internet-connected devices (for example, your computer connected to your Wi-Fi, or your phone connected to your mobile network) and Web-accessing software available on those devices (usually a web browser like Firefox or Chrome).

Servers are computers that store webpages, sites, or apps.

When a client device wants to access a webpage, a copy of the webpage is downloaded from the server onto the client machine to be displayed in the user’s web browser.

In addition to the client and the server, we also need to say hello to:

Your Internet connection: Allows you to send and receive data on the Web. It’s basically like the street between your house and the shop.

TCP/IP: Transmission Control Protocol and Internet Protocol are communication protocols that define how data should travel across the Web. This is like the transport mechanisms that let you to place an order, go to the shop, and buy your goods. In our example, this is like a car or a bike (or your own two feet).

IP Address:

An Internet Protocol address (IP address) is a numerical label assigned to each device (e.g., computer, printer) participating in a computer network that uses the Internet Protocol for communication. An IP address serves two principal functions: host or network interface identification and location addressing. Its role has been characterized as follows: “A name indicates what we seek. An address indicates where it is. A route indicates how to get there.

https://www.youtube.com/watch?v=7_-qWlvQQtY

DNS:

The computers that make up the Internet are set up in large networks that are identified and communicate using strings of numbers known as IP addresses. Since people lack the mental capacity to sort through and retain hundreds of numerical series, DNS is used to translate an actual name into these numbers.

Domain Name System Servers are like an address book for websites. When you type a web address in your browser, the browser looks at the DNS before retrieving the website. The browser needs to find out which server the website lives on, so it can send HTTP messages to the right place (see below). This is like looking up the address of the shop so you can access it.

http://learndns.com/

When you type in say www.example.com in the url your browser and your os will try to determine if they know what the ip address is already(it might be configured on your computer or might be in the memory –’cahce’) . Let’s assume that your OS has no idea what it is. Now what happens is your OS is configured to ask the Resolving Name Server (configured automatically or manually within your system)what the ip address is. And again assume that Resolving Name Server cannot find the ip address. All that your resolving server should know is where to find the root name server. Root name server->tld server(top level domain like .com, .in, .gov)—>Authoritative name servers. Now the ip address of  www.example.com are obtained from authoritative nameservers. The resolving name server takes the response from authoritative name servers and stores it in its cache and gives this to the operating system and now the operating system gives this ip address to the browser.

HTTP:

Hypertext Transfer Protocol is an application protocol that defines a language for clients and servers to speak to each other. This is like the language you use to order your goods.

Component files:

A website is made up of many different files, which are like the different parts of the goods you buy from the shop. These files come in two main types:

Code files: Websites are built primarily from HTML, CSS, and JavaScript, though you’ll meet other technologies a bit later.

Assets: This is a collective name for all the other stuff that makes up a website, such as images, music, video, Word documents, and PDFs.

So what happens, exactly?

When you type a web address into your browser:

  • The browser goes to the DNS server and finds the real address of the server that the website lives on(everything mentioned above –>fetching ip)
  • The browser sends an HTTP request message to the server asking it to send a copy of the website to the client (you go to the shop and order your goods). This message, and all other data sent between the client and the server, is sent across your internet connection using TCP/IP.

          What is HTTP?

HTTP stands for Hyper Text Transfer Protocol. Its an application layer protocol. It’s like a messenger on web. It’s a TCP/IP based protocol.

The 3 important things about HTTP is:

1)It’s connectionless–>After making the request the client disconnects from the server and then once the response is ready the server re-establishes the connection.

2)It can deliver any sort of data.

3)It’s stateless–>This means your Http request / response is not stored anywhere,i.e, the client and server know about each other only during the time of current request.

A HTTP requests consist of three sections:

1)Start / Request line->method(GET or POST)/uri/http version

2)Headers->consists of key-value pairs. The request-header fields allow the client to pass additional information about the request, and about the client itself, to the server. These fields act as request modifiers. For Example,

www.example.com is what I have typed in my URL.

My header might contain something like:

host:www.example.com

Accept:text/html

Accept-language:en-us

3)Body->Contains messages usually plain text(Binary at times)

GET /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: http://www.tutorialspoint.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

A HTTP response consist of three sections:

1)Status line-> http/version and status code. This status code tells the client if the request code has succeeded or not!(lke 202 OK or 404 Status not found)

1xx: Informational

It means the request was received and the process is continuing.

2xx: Success

It means the action was successfully received, understood, and accepted.

3xx: Redirection

It means further action must be taken in order to complete the request.

4xx: Client Error

It means the request contains incorrect syntax or cannot be fulfilled.

5xx: Server Error

It means the server failed to fulfill an apparently valid request.

Headers->The response-header fields allow the server to pass additional information about the response which cannot be placed in the Status- Line. These header fields give information about the server and about further access to the resource identified by the Request-URI.

  • Accept-Ranges
  • Age
  • ETag
  • Location
  • Proxy-Authenticate
  • Retry-After
  • Server
  • Vary
  • WWW-Authenticate

Body->Contains the requested file.

Here is an example :

HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
Content-Length: 88
Content-Type: text/html
Connection: Closed

 




Hello, World!

 



  • Provided the server approves the client’s request, the server sends the client a “200 OK” message, which means “Of course you can look at that website! Here it is”, and then starts sending the website’s files to the browser as a series of small chunks called data packets (the shop gives you your goods, and you bring them back to your house).
  • The browser assembles the small chunks into a complete website and displays it to you !Awesomeeeeeee!!

Artificial Intelligence-Gist

From the Metropolis, German sci-fi drama film released in 1927 to the ‘Droids’ of StarWars, ‘Skynet’ of Terminator, ‘TARS’ and ‘CASE’ from the Interstellar and the ‘Ultimagesron’ from the Avengers which were once just a figment of the imagination of some our most famous science fiction writers, artificial intelligence (AI) has made this dream come true.Artificial Intelligence (AI) is making computers do things that require intelligence when done by humans.Research in AI has focused chiefly on learning, reasoning, problem-solving, perception, and language-understanding. Alan Turing, British mathematician and WWII code-breaker, is widely credited as being one of the first people to come up with the idea of machines that think in 1950. The very premise of AI technology is its ability to continually learn from the data it collects. The more data there is to collect and analyze through carefully crafted algorithms, the better the machine becomes at making predictions. Artificial intelligence play a major role in email spam filtering, Google’s search predictions, and voice recognition, such Apple’s Siri or Microsoft’s Cortana and from robots investigating the progress of climate change to computers running the world’s finances. Not sure what movie to watch tonight? Don’t worry; Netflix has some suggestions for you based on your previous viewing experiences. Don’t feel like driving? Google’s working on a solution for that, too, racking up the miles on its driverless car prototype. From playing chess with the computer to Far Cry 2, AI is flourishing in the sphere of games .Google’s DeepMind(AI System) has conquered some big artificial intelligence challenges in its day, such as defeating Go’s world champion and navigating mazes through virtual sight. However, one of its accomplishments is that it has learned how to play soccer with a digital ant. IBM’s Watson beat two Jeopardy champions in a special edition of game show in 2011. In case you are woolgathering and forget to put a book back on a shelf after reading it, turning a monitor off when you’re done with a computer, putting milk back into the fridge, and getting food from the microwave, no worries because there are bots that take care of absent minded people. Fundamentally, the robot doesn’t know what a microwave is, or that milk goes bad: there’s no semantic understanding of the scene. All it’s doing, using unsupervised learning algorithms, is tracking patterns to detect forgotten actions. One of the most useful things about robots is that they don’t feel pain. Because of this, we have no problem putting them to work in dangerous environments or having them perform tasks that range between slightly unpleasant and definitely fatal to a human. And yet, a pair of German researchers believes that, in some cases, feeling and reacting to pain might be a good capability for robots to have. The researchers, from Leibniz University of Hannover, are developing an artificial robot nervous system to teach robots how to feel pain and quickly respond in order to avoid potential damage to their motors, gears, and electronics. They described the project last week at the IEEE International Conference on Robotics and Automation (ICRA) in Stockholm, Sweden. When it comes to business, AI are replacing humans. In fact, a Japanese venture capital firm recently became the first company in history to nominate an AI board member for its ability to predict market trends faster than humans. Tay, a Microsoft’s chat bot tweets , SeaSwarm that cleans up the oil spills in the ocean,Melomics that can compose music on bioinspired algorithms, there is so much potential for AI development that it’s getting harder to imagine a future without it. We’re already seeing an increase in workplace productivity thanks to AI advancements. AI will become commonplace in everyday life. After all, not everyone is ready for humanoid robots or self-learning spaceships.As technology advances to the point where machines have almost human-like capabilities, humanity is left to ponder the consequences involved with either advancing or holding back the field of computer sentience.The best way forward is for humans and machines to live harmoniously, leaning on one another’s strengths.