Wednesday, November 30, 2016

Howto Install Laravel Using Composer

Following are instructions to install Laravel Framework 5.3.16 on a Centos Linux 7. The PHP Composer 1.2.2 is used to install all the dependency packages. Detailed instructions on MS Windows environment were initially prepared in previous blog Howto Install Laravel Framework using Composer.

The system on a x86 64-bit computer consist of
CentOS Linux 7.2
HTTPD 2.4.6-40.el7.centos.4
PHP 7.0.13
MySQL 5.7.16-1.el7

Step 1. Install Laravel

Login to a command line terminal (CLI) as a normal user (with sudo rights) and type

 $ sudo yum install composer  
 $ composer -V  

This installs Composer 1.2.2. Minimum requirement is PHP 5.3.2 as mentioned as Composer website.

Install Laravel

 $ sudo yum install composer  
 $ composer -V  

Create your first application called hello.

 $> composer create-project --prefer-dist laravel/laravel hello   

This install Laravel 5.3.16 as shown in the image below. The folder tests and vendor is not shown.

Listing in hello folder.



Step 2. Run Artisan Serve

Start local artisan server

 $ cd hello  
 $ php artisan serve  

Open a web browser and point to the provided URL http://localhost:8000

To allow access to this instance with a domain name such as tboxmy.blogspot.com, start the server with following command

 $ php artisan serve --host= tboxmy.blogspot.com --port=8000  

Step 3. View the Results in HelloWorld

Test Laravel with a helloworld file

Open a terminal and go to the project folder ->hello ->resources ->views
Create a file helloworld.blade.php with following text

1:   <!DOCTYPE html>   
2:   <html>   
3:    <head>   
4:     <title>Laravel</title>   
5:    </head>   
6:    <body>   
7:   Hello World!   
8:    </body>   
9:   </html>   

Display this "VIEW" when the URL is /hello by editing the file web.php in the project folder ->hello ->routes

1:   Route::get('/hello', function () {    
2:     return view('helloworld');   
3:   });   

Save the file and start the artisan serve.

Open a web browser and type the URL mentioned above in Step 2. E.g.
http://localhost:8000/hello

This displays the Hello World! page with HTML formatting from the file resources/views/helloworld.blade.php

Done

Tuesday, November 22, 2016

Raspbian Command Line Basics 102

This is continuation from Raspbian Command Line Basics 101


Here are 18 commands on configurations of Raspbian Jessie or Release 8.0 on a Raspberry Pi 2. This might work for other versions of Raspbian, as they are common Linux commands in most cases.

At the Raspbian terminal, users can access a whole load of information. These can be useful for troubleshooting graphics, program conflicts and networking issues.

GENERAL

Display your device name and operating system information
$ uname -a

Change directory
$ cd directoryname

List files n a directory
$ ls
or
$ ls directoryname

Restart background services
$ sudo systemctl daemon-reload

Display disk usage or free space
$ free
$ free -h

Display disk usage by partitions
$ df -h

Display contents of a file
$ cat filename
e.g. to display CPU type
$ cat /proc/cpuinfo

Display the system date and time
$ date

Edit a file with Vim or Nano
$ vi filename
or
$ nano filename

Schedule shutdown at specific time
$ shutdown -h 21:00
or reboot
$ shutdown -r now

Configure Raspbian
$ sudo raspi-config

PACKAGE MANAGEMENT

List installed packages
$ dpkg-query -l

To clear up disk space before updating
$ sudo apt-get clean

Update Raspbian system packages
$ sudo apt-get update

Upgrade installed packages
$ sudo apt-get dist-upgrade

Install a package
$ sudo apt-get install mypackagename
or
$ sudo apt-get install mypackagename -y

Remove an installed package
$ sudo apt-get remove mypackagename
or complete removal with its configuration files
$ sudo apt-get purge mypackagename

Search for a package online
$ apt-cache search mypackagename

Display information of a package
$ apt-cache show mypackagename


NETWORKING

Default networking file is configured at
/etc/dhcpcd.conf

And the dhcpcd controls the networking interfaces

Display existing network interface settings
$ ifconfig


Display network routing table
$ route 
or display all numericals
$ route -n

Changes to the static ip in configuration file requires restart of the dhcpcd
$ sudo dhcpcd -k
$ sudo dhcpcd

Display status of running network interfaces
$ sudo service networking status

Restart network interfaces. Required after changes to network configuration.
$ sudo service networking restart

When there is network but web browser display "Unable to resolve name". Edit the domain name server at /etc/resolv.conf

nameserver x.x.x.x


Saturday, November 19, 2016

Oppo F1s How To Change Looks - Part 1

Oppo F1s is a decent phone running Android 5.1. Very responsive to the touch and with a nice big screen.

It's got a few things that new users just must do to personalize to look cute. Since there are so many things for customisation,  I will list few must know things to default theme.

Add screens

Default it comes with 2 screen where you can swipe left or right to go to each screen. Screens are a great way to organise contents. As you can see below,  there are 3 circles above the camera icon which means there are 3 screen.

Select an icon on the screen for the new screen. In this example,  I choose Theme Store icon and as drag it, the screen editor appears at bottom of screen.


Drag the icon to a new screen and release. If the icons are still wobbly or jello like,  tab on an empty space of the screen and it should end the editing mode. 



Create folder on a screen

Folders allow you to group several icons together on one screen.  With option to name the folder,  everything get more organised. Folders are shown as an icon with a little downwards arrow by it's side.

In order to create a folder,  identify the 2 icons for a folder.  Press on one icon and drag it on the centre of the other icon.  The icons will then be displayed in editing mode with the name of the folder above.  Click on that name and change the folder name.  Click the check mark and tap an empty space on the screen.

Change theme

Themes provide a feel of icon shapes,  background and lock screen that consistent.
Press Theme store icon and select a theme or press the download theme button.




The download button is to choose installed icons themes on the system.  Next press on apply and watch your screen and icon change. Other changes may effect fonts and lock screen.



Done.



Friday, November 11, 2016

Yum error Cannot retrieve metalink for repository

Yum provides installation, upgrades and removal of packages on Linux such as Centos. It is used to install other package repositories too. Over time, some of the certificates for installation would have changed and thats where you get this error when running Yum.

In this example, the error indicate Epel repository cannot be retrieved.

"Cannot retrieve metalink for repository: epel"

Some sysadmins have decided not to use that repo totally as they do not use that repo. E.g. when updating a package, they use

# yum --disablerepo="epel" update httpd

Solution

  1. Clean yum cache
  2. update the ca-certificate packages while disabling that repository with error.

Clean yum cache
# yum clean all

Update ca-certificate packages



In my Centos 6.3, it required to updated these related packages.


Monday, November 7, 2016

Howto Start Lavarel and MySQL


Lavarel provide the tools to quickly create an MVC application. In this guide, lets generate the required database table of a class and retrieve data while keeping to MVC.

Background

Models are used to represent objects and Laravel associates it with a database table of the same name. E.g. a model called Customer (singular) will have a table called Customers (plural). Using the --migration option will also produce a file to manage the database.
Our Customer class will consist of the fields id, cname, phone, last_update and a timestamp.



Controllers provide the glue to Views and Models. Laravel helps to create controllers that are empty or CRUD ready in the directory app/Http/Controllers. CRUD ready meaning that the standard functions or also known as Actions are generated for each HTTP action;

GET - create, show, edit and index.
POST - store
PUT/PATCH - update
DELETE - destroy

Each Controller file needs to be told which namespaces or Models to use.

Routing determines the URL mapping to actions that Controllers are to provide. Route can also be directly to a View or to deliver specific HTML contents without any Controllers.

The Laravel config directory contains a file, database.php with default settings to SQLite, Mysql and Postgresql database. You just need to fill in the appropriate values for the database in use. This Howto will use MySQL database.

Pre-Requisites

Installed the following and are in working order

  • Apache2
  • PHP v7.0.12
  • Composer 1.2.2
  • Lavarel 5.3


To install Laravel, refer to previous posting http://tboxmy.blogspot.my/2016/11/howto-install-laravel-framework-using.html

Step 1. Configure Lavarel to use MySQL

Create a database in MySQL called hellocustomer and assign the user permissions. Create 2 to 3 rows of dummy data as this howto will need to retrieve these data.

Edit the file hello/config/database.php and configure database

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'hellocustomer'),
            'username' => env('DB_USERNAME', 'mysql'),
            'password' => env('DB_PASSWORD', 'somepassword'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

Edit hello/.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=hellocustomer
DB_USERNAME=mysql
DB_PASSWORD=somepassword

Step 2. Create the Customer Model

1. At the project workspace directory, open a terminal and type

php artisan make:model Customer --migration

This will create

  • hello/app/Customer.php
  • hello/database/migrations/YYYY_MMM_DD_###_create_customers_table.php


2. Edit the table file in migrations (above) with all the required fields for Customer model.
3. Activate the migration file to generate Customer table in the database. Just type;

php artisan migrate

If the schema needs to be changed and database deleted after the above was created, just refresh the database, with;

php artisan migrate:refresh


Step 3. Create the CustomerController class

In order for the CustomerController class to retrieve data from the database, Eloquent provides the all() function, such as;
 $customers = Customer::all();  

An array of data rows are extracted to the variable $customers. No additional SQL coding is required.

At the terminal, type the following to generate the controller class app/Http/Controllers/CustomerController.php

php artisan make:controller CustomerController --resource

Edit CustomerController to use the Customer model by adding the "use App\Customer;" above class declaration as below;


use App\Customer;
class CustomerController extends Controller
{
 public function index()  
 {  
       //              
       $customers = Customer::all();  
       return View('customers.index')->with('cust', $customers);  
 }  

 public function show($id)  
 {  
       //              
       $customer = Customer::find($id);  
       return view('customers.show', array('customer' => $customer));  
 }  


Step 4. Create View for the Implemented Actions in Controller

Create a directory customers in resources/views and create the following 2 files

 <!DOCTYPE html>  
 <?php  
 // index.blade.php   
 ?>  
 <html>  
 <head>  
   <title>Customer index page</title>  
   <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">  
 </head>  
 <body>  
 Customers  
 <br/>  
 @foreach($cust as $key => $value)  
 <table>  
     <tr>  
       <td>{{ $value->id }}</td>  
       <td>{{ $value->cname }}</td>  
       <td>{{ $value->phone }}</td>  
                   </tr>  
                   @endforeach  
 </table>  
 </body>  

 <!DOCTYPE html>  
 <?php  
 // show.blade.php   
 ?>  
 <html>  
  <head>  
   <title>Customer</title>  
  </head>  
  <body>  
  <?php  
       if ($customer == null)   
       {  
             print "No data";  
             return;  
       }  
       ?>  
   <h1>Customer {{ $customer->id }}</h1>  
   <ul>  
    <li>Name: {{ $customer->cname }}</li>  
    <li>Phone: {{ $customer->phone }}</li>  
    <li>Last update: {{ $customer->last_update }}</li>  
   </ul>  
  </body>  
 </html>  

Edit routes/web.php file to use this new controller.

Route::resource('customer', 'CustomerController');

Step 5. Try them out

The Actions from our Laravel framework implementation can be listed with the command

php artisan route:list







Since you now know the URL for each Action we know to do the following displays for all customers and for each customer.

To call  INDEX action use the URL
http://localhost:8000/customers/



To call  SHOW action use the URL followed by a valid customer id.
http://localhost:8000/customers/1


Exercise:

This should take less than 15 minutes to complete.

Demonstrate use of database with Laravel. Create following Customer class below and display formatted data (create your own dummy data in the database).










Friday, November 4, 2016

Howto Install Laravel Framework using Composer

Laravel framework provides rapid development of a model-view-controller (MVC) with PHP programming language. Currently is at version 5.3 and is open source using MIT license.

Composer is a tool to easily retrieve packages required for most PHP projects and frameworks. Laravel can be found available in Composer at packagist.org. Detailed documentation for Composer can be found at https://getcomposer.org

This is a quick guide on installing Laravel then creating a page to display hello world!.

Installation environment for this How-to;
MS Windows 10
Apache2
PHP v7.0.12
Composer 1.2.2
Laravel 5.3

By the end of this guide, the following directories should have been generated.


Step 1: Installing Composer

Ensure Apache2 and PHP7 is already installed and working before starting this guide.

Download and install Windows Composer installer https://getcomposer.org/Composer-Setup.exe

Open a terminal and change to the installed PHP folder. Type

 $> composer -V  

(Or with the file composer.phar in same directory, type)
 $> php composer.phar  
This should display the version number of composer.
Update composer and check the version.
 $> php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"  
 $> php -r "if (hash_file('SHA384', 'composer-setup.php') === 'aa96f26c2b67226a324c27919f1eb05f21c248b987e6195cad9690d5c1ff713d53020a02ac8c217dbf90a7eacc9d141d') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"  
 $> php composer-setup.php  
 $> php -r "unlink('composer-setup.php');"  
 $> composer -V  

Step 2: Create an empty composer project 

Create a project folder called myproject.
Open a terminal and enter folder for myproject
Type
 $> composer init  

Choose default values, except following;
Package type: project
dev dependencies: yes
Search for a package: laravel/laravel

Press "Enter" until exit the interactive menu.

This will create a default json file for the composer. This step is only an example of creating a json file. Feel free to ignore this step.

Step 3: Install Laravel Framework and dependencies

Create a project folder called myproject, if you haven't.
Open a terminal and enter folder for myproject, and type.

 $> composer create-project --prefer-dist laravel/laravel hello  


This will do pretty much that same as Step 2 above, except that it will create the workspace called hello and load all the required libraries and plugins.

Laravel is installed in the folder hello.

Step 4: Start local server

Open a terminal and enter folder for hello, and type.

 $> cd hello  
 $> php artisan serve  

Laravel development server started on http://localhost:8000/

Open a web browser and type the URL mentioned above.
http://localhost:8000/

It displays the default Laravel page.

Step 5: Display a Hello World page

Open a terminal and go to the project folder ->hello ->resources ->views
Create a file helloworld.blade.php with following text

 <!DOCTYPE html>  
 <html>  
   <head>  
     <title>Laravel</title>  
   </head>  
   <body>  
  Hello World!  
   </body>  
 </html>  

Tell Laravel that to display this "VIEW" when the URL is /hello.
Edit the file web.php in the project folder ->hello ->routes

 Route::get('/hello', function () {    
      return view('helloworld');  
 });  

Save the file.

Open a web browser and type the URL mentioned above.
http://localhost:8000/hello

This displays the Hello World! page with HTML formatting from the file resources/views/helloworld.blade.php

Step 6: Display a Hello World text (without Views)

Tell Laravel to display some HTML when the URL is /hello2
Edit the file web.php in the project folder ->hello ->routes

 Route::get('/hello2', function () {    
      return 'Hello World 2!';  
 });  

Save the file.

Open a web browser and type the URL mentioned above.
http://localhost:8000/hello2

This displays the Hello World 2! text.

Happy trying out Lavarel.



Blog Archive