Laravel With Debian Stretch

Laravel Workshop

Introduction

  • Early days of dynamic web,writing web application looked alot of different than it look today.
  • Developers need to write both business components and general purpose components like user authentication,input validation,database access and templating
  • But today alot of application development frameworks and thousands of components and libraries easily available.
  • The problem is by the time you learn one framework , better three will appear in the industry.
  • The question is why we need to use a framework and then more specifically why laravel?

Why use a framework

  • frameworks prepackage a collection of third party components together with custom framework glue like configuration files,service providers,prescribed directory structures and application bootstraps.

Benefit of using a frameworks

  • Someone has made decisions not only about individual components for you but also about how these components should fit together.

Suppose that I am not using any frameworks at all

  • Will create an application from scratch
  • Start by routing and you have made all decisions on how the application works.
  • More specifically the application is designed in your custom framework.
  • You need to answer all the questions how these should work to everyone.
  • What about the impact on next developer?
  • what about four such custom framework based application?

consistency and flexibility

  • A framework address these issues by
  • answering which components should we use here.
  • and ensuring that chosen components work well together.

Additionally

  • They provide conventions that reduce the amount of code a developer new to the project has to understand.
    eg. if you known how routing works in one laravel project, you understand how it works in all laravel projects.

But we also need

  • Ability to control what does and doesn’t go into your applications foundation.
  • Also give you the freedom to customize to your heart’s content.Here is where laravel is special about.

A short history of Web Frameworks

  • ruby on rails (2004)
    first popular framework that has really inspired all other frameworks
    introduced concepts like mvc,restful json api,convention over configuration,activerecord
    really impact on rapid application development.
  • php framworks
    getting inspired from ruby on rails
    cake php (2005)
    symfony,codeigniter,zend,kohana
    yii(2008)
    aura,slim(2010)
    fuelphp and Laravel(2011)
  • While frameworks like symfony and zendfocussed on enterpise design patterns and ecommerce, others focussed on orms,mvc and tools for rapid application development.
  • good and bad of codeigniter
    • popular until 2010
    • its use of modern technology and patterns advanced slowly
    • as framework world grew and php advanced,ci fall behind in both technological advances and out of boxfeatures
    • It is managed by a company who were slow to catch up with advanced php features like namespaces, moves to github and later to composer.
    • interstingly, taylor otwell ,laravel crator-dissatified with ci and set of to write his own framework.
  • Laravel 1,2 and 3
    1st beta released on june 2011 which was completely from scratch
    2 and 3 released quickly in the following november and february
  • Laravel 4
    • At that time composer was showing signs of becoming an industry standard,otwell completely rewrote the entire framework as a collection of components ,distributed and bundled together by composer.
    • He developed components under the name code name illuminate
    • Instead of bundling majority of its code as a download,laravel now pulled in majority of its components from symfony and illuminate through composer.
    • Laravel 4 introduced mail,facades,database seeding
    • Interestingly it is mirroring 6 months after symfony release
  • laravel 5
    as 4.3 but since it is a major change ,introduced as 5
    feb 2015

What is so special about Laravel?

  • Why not symfony, since it uses components released by symfony ?
  • Laravel’s Philosophy is to increase developer speed and developer happiness.
  • Currently programmers spend huge amount of time for making the code look pretty, just for better experience of looking at the code itself.But that process waste alot of time.
  • Laravel at its core, is all about equipping and enabling developers.
  • Its goal is to provide clear,simple and beautiful code and features that help quick learn,start and develop.
  • Resulting code that will last.

Lets Get Our hands Dirty.

  1. Installing php and other php dependencies for laravel
    • php7.0
      php7.0-common
      php7.0-cli
      php7.0-mbstring
      php7.0-xml
      php7.0-zip
  2. Install composer which is the php package manager
    • apt-get install composer
  3. create the laravel project
    • This can be done in two ways.
    • using composer create-project
      • composer create-project  laravel/laravel  blog
    • using laravel install
      • composer global require “laravel/installer”
      • add installer binary to our path variable
        • nano .bashrc
        • add the following line
        • export PATH=”$PATH:$HOME/.config/composer/vendor/bin”
        • save and reopen terminal and echo $PATH
        • laravel new blog
  4. choose an IDE—Here we choose atom
  5. launch the application
    1.php artisan serve
    2.how the front page works.
    3.create our first route. eg
    4. Route::get(‘/test’,function(){ echo “Hello world”; });
    5. Route::get(‘/test’,function(){ return view(‘test’)});
    6. Http verbs–GET,POST,PUT,PATCH,DELETE,OPTIONS
    7.ref: https://laravel.com/docs/5.4/routing
  6. Introduction to MVC
    1.Model
    2.View
    3.Controller.
  7. Database Configuration
    1. apt-get install mysql-server php7.0-mysql
    2. login to database using terminal– mysql -u root -p  <enter>  //empty password
    3. create a database with admin privileges
      1. CREATE USER ‘admin’@’localhost’ IDENTIFIED BY ‘space123’;
      2. GRANT ALL PRIVILEGES ON *.* TO ‘admin’@’localhost’ WITH GRANT OPTION;
      3. FLUSH PRIVILEGES;
      4. test created user with terminal login–mysql -u admin -p
    4. apt-get install phpmyadmin
      1. configure webserver as apache and account created above.
      2. login to phpmyadmin < localhost/phpmyadmin> with admin credentials.
      3. create a database
    5. Configure Laravel with the database settings
      1. Edit the .env file
      2. database,username, and password parameters.
  8. Migrations
    1. Feature in Laravel to store the database schema
    2. Found in database/migrations.
    3. Php artisan migrate
      1. result in an error
      2. error key length due to
      3. https://laravel.com/docs/master/migrations#creating-indexes
      4. Edit  “appservice provider” file with
        use Illuminate\Support\Facades\Schema;public function boot(){
        Schema::defaultStringLength(191);
        }
      5. Manually delete the created databases.
      6. php artisan migrate.
      7. migrate:reset, migrate:refresh, migrate:rollback –steps=2
  9. Inbuilt authentication plugin in Laravel
    1. php artisan make auth
  10. Basic Task List
    1. ref: https://github.com/laravel/quickstart-basic
    2. Artisan
    3. Php artisan make:migration test_migration.
    4. Php artisan make:migration create_tasks_table  –create=tasks
    5. camelcase
    6. $table->string(‘name’);
    7. php artisan migrate.
  11. Database Seeding
    1. Feature in Laravel , to persist data in the application.
    2. php artisan make:seeder TasksTableSeeder
    3. If there are guarded attributes, disable them temporarly
    4. Model::unguard() // seeding code// Model::reguard()
    5. DB::table(‘tasks’)->insert([‘name’ => ‘task1’ ]);
    6. see Mass Assignment In Laravel
    7. Inside database seeder class under run method
    8. $this->call(TasksTableSeeder::class);
    9. php artisan db:seed
    10. or  with <php artisan migrate:refresh –seed>
    11. repeat twice.
    12. what about timestamps?
    13. Actually seeder to be used with Model Class
  12. Models & Eloquent ORM
    1. Models represent the database handling code/class in Laravel
    2. Why Model objects, just use relations
      1. MVC pattern is based on OOP Concepts
      2. Why Objects ? Why not relations ?
      3. Problem in Using Objects only?
      4. Significance of ORM
      5. Eloquent and Model Naming Conventions
    3. Model Handling
      1. php artisan make:model Task
      2. Eloquent
      3. seeding using eloquent and timestamps
        \App\Task::create([‘name’=>’flight1’]);
      4. Task and tasks
    4. Tinker
      1. Read-Evaluate-Print-Loop for PHP
      2. php artisan tinker
        1. eg. $tasks=\App\Task::all();
  13. Controller in Laravel
    1. app/http/controllers
    2. php artisan make:controller TaskController
  14. Task list second stage
    • Implement list operations
      • define route ( Route::get(‘/tasks’,’TaskController@list’)
      • define controller function
        • public function list(){  $tasks = \App\Task::all();  return view(‘list’)->with(‘jobs’,$tasks); }
      • define view as list.blade.php
        • @foreach($jobs as $job)
          • {{$job->name}}
        • @endforeach
  15. Blade Templating Engine
    1. Blade directives
    2. Blade Layout extension
  16. Task creation
    1. route…controller..form…route…controller…save
    2. csrf token and form security———— {{ csrf_field() }}
    3. public function save(Request $request)
      {
      $task = new \App\Task;
      $task->name= $request->name
      $task->save();
      return \Redirect::to(‘/tasks’);}
  17.  Resources and Controller as a Resource Manager
    1.  php artisan make:controller TaskController  –resource
    2. Route::resource(‘tasks’, ‘TaskController’);
    3. php artisan route:list
    4. & identify the changes.
  18. Edit Implementation
    1. CRUD and POST,GET,PUT/PATCH,DELETE.
    2. Form Method Spoofing
    3. {{ method_field(‘PUT’) }}
  19. Delete Implementation
    1. destroy eloquent method
    2. {{ method_field(‘DELETE’) }}
    3. return redirect()->route(‘tasks.index’)
  20. Plugin Usage
    1. eg Laravel DomPDF Plugin
    2. https://github.com/barryvdh/laravel-dompdf
    3. update composer and add the package
    4. composer require barryvdh/laravel-dompdf
    5. add the following line to the providers array in config/app.php
    6. Barryvdh\DomPDF\ServiceProvider::class,
    7. optionally add following line to aliases array in config/app.php
    8. 'PDF' => Barryvdh\DomPDF\Facade::class,
    9. Publish configuration file for dompdf ( config/dompdf.php will be generated)
    10. php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"
    11. create a route /pdf…route to a controller function —and enter the following code
      1. $pdf = App::make('dompdf.wrapper');
        $pdf->loadHTML('<h1>Test</h1>');
        return $pdf->stream();
      2. check to see if the pdf is being generated.

Leave a comment