/laravel-firstapp

Documentation what I've got during studying Laravel 5.5 by myself

Primary LanguagePHP

Table of contents

Inspired by Laravel udemy course

Reference:

Lavarel MVC

  • Model: database objects

  • View: html

  • Controller: Middle-Man

Environment Setup

  • Lavarel 5.5 requires PHP 7+

  • XAMPP or Lavarel Homestead or Laragon ?

    • XAMPP: enable htdocs folder visible: Volumes -> Mount

       alias htdocs="cd $USER/.bitnami/stackman/machines/xampp/volumes/root/htdocs"
    • Lavarel Homestead tutorial - link

  • PhpStorm

  • Packagist: php package repository - like java arifactory

  • Composer: php package manager

    • Installation: https://getcomposer.org/download/

      • add into zshrc/bashrc:

        export PATH="$PATH:$HOME/.composer/vendor/bin"
    • Grant global access

      mv ~/.composer.phar /usr/local/bin/composer
      chmod u+x /usr/local/bin/composer
    • htdocs folder: Create a project

      • by composer

        composer create-project --prefer-dist laravel/laravel firstapp 5.5.28
      • by laravel installer

        composer global require "laravel/installer"
        lavarel new firstapp
        
      • grant access for storage folder after created

        chmod -R o+wr firstapp/storage
    • show versions laravel/laravel from repository

      composer info -a laravel/laravel
  • Virtual Host: enter the website by laravelfirstapp.dev instead of localhost/laravel-firstapp/public

    • xamppfiles/etc/httpd.conf: enable httpd-vhosts.conf

      # Virtual hosts
      
      Include etc/extra/httpd-vhosts.conf
      
    • xamppfiles/etc/extra/httpd-vhost.conf

      replace the content with

      # localhost
      
      <VirtualHost *:80>
          ServerAdmin webmaster@dummy-host2.example.com
          DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs"
          ServerName localhost
          ServerAlias www.localhost
      </VirtualHost>
      
      # laravelfirstapp.dev
      
      <VirtualHost *:80>
          ServerAdmin webmaster@dummy-host2.example.com
          DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/laravel-firstapp/public"
          ServerName laravelfirstapp.dev
      </VirtualHost>
    • vim /etc/hosts

      127.0.0.1	laravelfistapp.dev
      
  • How to fix common errors ?

    • Unable to access phpmyadmin ? Enable access phpmyadmin

      # etc/extra/httpd-xampp.conf
      <Directory "/opt/lampp/phpmyadmin">
          AllowOverride AuthConfig Limit
          Require all granted
          Order allow,deny
          Allow from all
      # Require local
      # ErrorDocument 403 /error/XAMPP_FORBIDDEN.html.var
      </Directory>
    • Your connection is private on Chrome ?

      use FireFox - or create host name without ending with dev

    • Got forbidden access ?

      • sure that this is not enable
      # Virtual hosts
      #Include etc/extra/httpd-vhosts.conf
      

For debug

return $var; //json output
var_dumps($var); 
dd($var);
dump($var);

Lavarel Basic

Framework Structure

  • config/app.php:

    • configuration: app name, env, locale, debug,...
    • import providers - 3rd apps
  • config/database.php: database connection configuration

  • .env: properties file, get value by functionenv($key, $default = null)

  • vendor: dependencies installation folder

  • app: events, exceptions, http, ...

  • artisan: show available commands

    php artisan
    

Routes

  • routes/web.php

  • Routes folder: view function mapping to resources/views/**.blade.php

    • e.g: route with path variables
    Route::get('/students/{id}/{name}', function ($id, $name) {
        return "Hello student with id: ". $id ." - name: ". $name;
    });
  • List all routes:

    php artisan route:list
    
  • Naming a route

    Route::get('/teachers', array('as' => 'admin.home', function () {
        $url = route('admin.home');
    
        return "your url: " . $url;
    }));
    
    Route::get('/post/{id}', ['as' => 'home.post', 'uses' => 'AdminPostsController@post']);
    
    // if you dont want use default generated urls
    Route::resource('/admin/users', 'AdminPostsController@post', ['names' => [
        'index' => 'your.custom.route'
        'store' => 'your.custom.store'
        'create' => 'your.custom.route.create'
        'edit' => 'your.custom.route.edit'
        'update' => 'your.custom.route.update'
        'delete' => 'your.custom.route.delete'
    ]])

Controller

  • Create a controller:

    • Normal controller

      php artisan make:controller StudentsController
    • Controller for show, update, edit, store,... :

      php artisan make:controller --resource StudentsController
  • Mapping a route to controller method

    Route::get('/student', 'StudentsController@index');
  • Passing & retriving data

    Route::get('/student/{id}', 'StudentsController@index');
    
    ....
    class StudentsController {
        public function index($id) {
    
        }    
    }
  • Resource route: generate full REST-API: GET-index, GET-create, GET-show, GET-edit, POST, PUT/PATH, DELETE methods by just a single line of code.

    Route::resource('/student', 'StudentsController');
  • Docs: https://laravel.com/docs/5.5/controllers

View

  • Blade file: template engine

  • Create a custom view and custom method

    • Add a method in the controller
    • Add a route for that method in routes/web.php
    • Create blade php file in resources/views
  • Passing data to views

    • web.php

      Route::get('/contact/{name}', 'StudentsController@contactStudent');
    • Controller

      public function contactStudent($name)
      {
      	return view('pages.contact-student')->with('name', $name);
      	
      	# or
      	# return view('pages.contact-student', compact('name'));
      }
    • View

      <h1>Hello {{$name}}</h1>
  • Docs: https://laravel.com/docs/5.5/views

Create master layout

  • create layouts/app.blade.php: @yield('content'), @yield('footer')
  • using:
    • @extends('layouts.app')
    • @section('content') ... @endsection

Blade template engine

@if(count($people))
    <ul>
        @foreach($people as $person)
            <li>{{$person}}</li>
        @endforeach
    </ul>
@endif

Setup project clone from git

composer install && composer update;
chmod -R o+rw storage;
composer run post-root-package-install;
composer run post-create-project-cmd;

npm install;
npm run dev;

// refreshing cache
php artisan clear-compiled
composer dump-autoload
php artisan optimize

Maintainance and live mode

php artisan down
php artisan up

Recommended tools

Install xdebug or http://phpdebugbar.com/ or https://tracy.nette.org/en/

https://dev.to/david_j_eddy/5-php-tools-to-make-your-life-more-enjoyable-1jl1

https://www.phpdoc.org/

PHPStorm - Jetbrains