Friday, February 09, 2018

Laravel quick application tutorial

Ok, so we have installed Laravel. Now let's start coding our CRUD (create/read/update/delete) application.

First, we will create our new application so go to a directory of choice and type:
laravel new market
Laravel will create a new directory with the name market and place all its framework files inside, ready to work. Now go to this directory with cd market and let start our web server on port 8000 with:
php artisan serve
Then open up the browser at: http://localhost:8000 to see the default laravel installation page.

Models
Now let's create our database and let laravel manage the tables inside by doing migrations and creating models around the database logic.
In order to create the database we will use our mysql client:
mysql -u root -p
(where username is root, here you can use the user which mysql is installed on your system)
Next, we will create our main database:
create database market
then exit the mysql client
and we will connect laravel to the database. For this open up the environment file: /market/.env and set up mysql user and password sections with your own.
 
Laravel uses ORM (Object-relational mapping) to abstract database table operations, so it wraps models around the tables, making ordinary operations easier via php objects. Models reside in the directory: market/app/model.php.
Let's create our first model:
php artisan make:model -m Product
(here the command creates table products (in plural) and model Product, connected to it).
You can use phpmyadmin to see the newly created table.

Migrations
Next, we need to specify the fields we would like to have in our tables. By using migrations we can change the structure of our tables add/change/remove fields and their properties. Migrations are text files residing in: market/database/migrations
If you look inside a migration file there are usually two functions up()->when the table will be created and down() for when the table will be deleted.
Inside up() place:
$table->increments('id');
$table->string('title')->unique();
$table->string('description');
$table->integer('price');
$table->timestamps();
next to perform the actions described in the migration we will just refresh (redo) the performed migrations so type:
php artisan migrate:refresh
you can go ahead and check within phpmyadmin that the new table's structure is actually changed having more fields inside.

Controllers
In order to work with the data from the tables laravel uses controllers. Let's create one:
php artisan make:controller -m Product ProductController
with this command, we create ProductController file connected to our Product model. Controllers reside in app/Http/Controllers/ . We can see our file there, it is already prepared with the basic actions as functions() we would like to fill in.

Routes
One way for the user to trigger a controller action is via routes (routes/web.php). Routes connect an URL with a controller action or a view. So open up the file and replace the default: return view('welcome'); with: return 'Hello';
then refresh the browser and see the change.
Now, let's replace the file contents with:
Route::resource('products','ProductController');
this will create a so-called resource-route which will connect our ProductController to url: /products
In order to see how many rules this resource-route handles type: php artisan route:list



We see that whenever we access /products it invokes the index() method of ProductsController. From the table you can follow the rest of the urls and their representative actions.
Next, we modify the controller's index action and trigger a view from there:
$products="Hello from products"
return view('products.index',['products'=>$products]);
to decrypt :) this will invoke a view file from directory view/products called index.blade.php and pass $products as a parameter to it.
Now is time to create this directory market/resources/views/products.
As well as the index.blade.php inside, with the following content to display the products:
 {{$products}} 
( Using double curly braces in the view we could output parameters passed from the controller. )
 
Please test in the browser.
Next, create some fake products inside phpmyadmin and in order to see them just replace in our controller:
$products=Product::all();
(this is the ORM way of doing it)


Layouts
So far we have only displayed the products list, so let's extend the current view. First we will explore how to work with layouts. They allow us to include common HTML and CSS parts across multiple views, reducing potential code repetition.
In order to use layouts create app.blade.php into views/layouts folder and place all your main HTML including CSS links there. Inside put also: yield('main'). This yield will become a placeholder where Laravel will include all information fetched from the .blade view files.
The following is our completed view index.blade.php file which uses our layouts.app layout and inside its 'main' section inserts:

@extends('layouts.app')
@section('main')

<a href="{{route('products.create')}}" class="btn">Add new product</a>
<br/>

@foreach($products as $product)
<div class="products">
<a href="{{route('products.show',$product)}}">{{ $product->title }} </a> - ${{ $product->price }}  <br/>
<br />

<a href="{{route('products.edit',$product)}}" class="btn">Edit</a>

<form method="post"  action={{route('products.destroy',$product)}} >
{{method_field('DELETE')}}
{{csrf_field()}}

<input class="inline btn" type="submit" value="Delete" />

</form>

<br /><br/>
</div>
@endforeach

@endsection

In short, we have added buttons for adding, editing and deleting products using a loop for each of our products. We are also using the internal Laravel function route() in order to construct user clickable urls, taken from the route list table discussed above. The route generation in some cases requires additional parameter like $product information in order for Laravel to know which product to work with. For security reasons, the delete button functionality uses a form. Inside you can see uniquely generated csrf() field to ensure that we could only send the form thus protecting it from malicious usage. Lastly in order to make the form to function Laravel requires to modify or model and add the fields allowed to be mass-modified. So in /app/Product.php add
protected $fillable=['title','description','price','image_url'];
So go ahead save, and let's move on.

Adding new products
please add to our ProductsController:
 public function create()
    {
        return view('products.create');
    }
which will show the create.blade.php:
@extends('layouts.app')
@section('main')

@if ($errors->any())
<ul>
    @foreach ($errors->all() as $error)
    <li>{{$error}}</li>
    @endforeach
</ul>
@endif

<form action="{{ route('products.store') }}" method="post" >
{{csrf_field()}}
<label for="title">Product Name</label>
<input placeholder="product 1" type="text" name="title" id="title" />

<label for="description">Description</label>
<input placeholder="product description" type="text" name="description" id="description" />

<label for="price">Price</label>
<input placeholder="100" type="text" name="price" id="price" />

<button type="submit">Create</button>

@endsection

By far the code looks familiar, the first few lines just display if any errors occur while adding our product. We notice that when submitted, this form goes to products.store route, leading to our controller. So let's create the corresponding ProductController method:
   public function store(Request $request)
    {
        $customMessages = [
            'title.unique'=>'Please choose another product title'
        ];

        $this->validate($request,
            ['title' => 'required|unique:products'],
            $customMessages
        );
    
        Product::create($request->all());
        return redirect('products');
    }

Here we have included a custom message to be displayed when the 'title' of the product about to be entered already exists in the database ->yes Laravel do this for us automatically. If everything is ok, the product is created, using all the parameters sent from the $request and we display the product view.

Editing products
In the ProductController we would like to trigger the edit.blade.app view:
  public function edit(Product $product)
    {
        return view('products.edit',['product'=>$product]);
    }

wherein edit.blade.app:

@extends('layouts.app')
@section('main')

@if ($errors->any())
<ul>
    @foreach ($errors->all() as $error)
    <li>{{$error}}</li>
    @endforeach
</ul>
@endif

<form action="{{ route('products.update',$product) }}" method="post" >
{{csrf_field()}}
{{method_field('patch')}}
<label for="title">Product Name</label>
<input placeholder="product 1" type="text" name="title" id="title" value="{{$product->title}}" />

<label for="description">Description</label>
<input placeholder="product description" type="text" name="description" id="description" value="{{$product->description}}" />

<label for="price">Price</label>
<input placeholder="100" type="text" name="price" id="price" value="{{$product->price}}"/>

<button type="submit">Update</button>

@endsection

The update form above is being pre-filled with information about the product we already have, using the {{$product->property}} notation. We have also included the 'patch' method, as described in the route table above in order for Laravel to trigger the ProductController update method:

 public function update(Request $request, Product $product)
    {
        $customMessages = [
            'title.unique'=>'Please choose another product title'
        ];

        $this->validate($request,
            ['title' => 'required|unique:products'],
            $customMessages
        );

        $product->update($request->all());
        return redirect('products');
    }

We see almost the same code as in the create method, just using the update() function.

Deleting products
For the delete button, we don't need a view, here is just its code in the controller:
   public function destroy(Product $product)
    {
        $product->delete($product->id);
        return redirect('products');
    }
We believe that by now you can do the view method very easy!

That's it! Enjoy!

Wednesday, July 26, 2017

Install PHPMyAdmin and Nginx on Ubuntu 19.04

Knowing how to install PHPMyAdmin and Nginx is essential when developing web applications, it is also very useful while taking comprehensive course projects, which prefer local installations.

References: Practical Ubuntu Linux Server for beginners

You can watch the video on the subject: 

So let's start first the installation of Nginx:
sudo apt-get install nginx
(optional) check if its ports are enabled in the firewall: sudo ufw status
and enable port 80 if necessary:
sudo ufw allow 'Nginx HTTP'
find and note your ip address with: ifconfig

open up in browser: http://127.0.0.1 and you should see: Welcome to NGINX

now is time to install MySQL-server:
sudo apt-get install mysql-server
remember to set a password!

(optional) secure mysql: sudo mysql_secure_installation


change the connection of MySQL from a socket to password authentication and set its password to password (to be able to use the root account within PHPMyAdmin):
sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
next, apply the changes with:
FLUSH PRIVILEGES;
EXIT


install php-fpm to enable Nginx to communicate with PHP, as well as php-mysql to enable PHP to communicate with MySQL databases:
sudo apt-get install php-fpm php-mysql
(optional) security for PHP (fix closest name script execution): sudo nano /etc/php/7.3/fpm/php.ini (replace /php/7.x with your PHP version (type: php -v)
inside php.ini change cgi.fix_pathinfo=0

enable Nginx to process PHP:
sudo nano /etc/nginx/sites-available/default

server {

listen 80 default_server;

listen [::]:80 default_server;

root /var/www/html;

#add index.php for processing PHP files first

index index.php index.html index.htm index.nginx-debian.html;


#you can replace 127.0.0.1 with your own ip address

server_name 127.0.0.1;


location / {

try_files $uri $uri/ =404;

}

#the actual PHP processing

location ~ \.php$ {

#enable only these two:

include snippets/fastcgi-php.conf;

fastcgi_pass unix:/run/php/php7.3-fpm.sock;

}


location ~ /\.ht {

deny all;

}

}


reload Nginx configuration:

sudo systemctl reload nginx

(optional) if problems occur: test the configuration with: sudo nginx -t


test if php is running: by creating a test file:

sudo nano /var/www/html/test_php.php

<?php

echo "php is running";

?>

save and exit with ctrl + W and ctrl + X


(optional)(if you don't have nano: sudo apt install nano )


point the browser to 127.0.0.1 you should get: php is running


Final step:

Install PHPMyAdmin (remember your MySQL password)
sudo apt-get install phpmyadmin
select apache2 installation from the dialogue.

add PHPMyAdmin configuration into Nginx in order enable /phpmyadmin directory location (127.0.0.1/phpmyadmin)
sudo nano /etc/nginx/sites-available/default
then add into the server directive:


location /phpmyadmin {

root /usr/share/;

index index.php index.html index.htm;

location ~ ^/phpmyadmin/(.+\.php)$ {

try_files $uri =404;

root /usr/share/;

fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include /etc/nginx/fastcgi_params;

}

location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {

root /usr/share/;

}

}



location /phpMyAdmin {

rewrite ^/* /phpmyadmin last;

}


notice the php7.3-fpm.sock -> replace it with your PHP version!


Here you go you are ready to run: 127.0.0.1/phpmyadmin
you can log in with "phpmyadmin" and "your_mysql_password"
( if you don't have permissions to create databases log as root and "your_mysql_password" )

phpmyadmin running on nginx in ubuntu

Cheers!

Subscribe To My Channel for updates