Skip to content

Instantly share code, notes, and snippets.

@misostack
Last active March 1, 2024 15:40
Show Gist options
  • Select an option

  • Save misostack/21dfd8208130c0576f695664fa20c140 to your computer and use it in GitHub Desktop.

Select an option

Save misostack/21dfd8208130c0576f695664fa20c140 to your computer and use it in GitHub Desktop.
headfirst-mvc-laravel

Brainstorming

Headfirst MVC

  • Targets : leaning and investigate MVC and Relation DB Design with Laravel(php) and MySQL

Sample project

Jim is a lecturer at a community college, and he wants an application to help him to keep track of the students he is advising. He wants to know which students register for which courses in which semester and what grade they got in those courses. He also wants the application to be password protected, with each user having a different login and password, and it must keep track of user lo gins/outs. Your job as a database developer is to design a database that will serve as the back-end of either a web-based or client-server based application. This database must capture and store all the relevant persistent data that will be used by the application

Todo

First step

  1. Install PHP lastest version
  2. Setup Empty Laravel Project
  3. Setup Dev Enviroment for laravel and Learn the basic elements
  4. Design Relational DB
  5. Setup Test Enviroment and design test strategies
  6. Design

Second step

  1. CRUD forms
  2. Layout and templates ( bootstrap )
  3. Modified CRUD forms with new UI
  4. Simple Authentication : Login
  5. Migration : Data seed
  6. CRUD Users

Third step

  1. Service: mailer
  2. Ion Authentication: login,reset password

Analyzing

Jim is a lecturer at a community college, and he wants an application to help him to keep track of the students he is advising. He wants to know which students register for which courses in which semester and what grade they got in those courses. He also wants the application to be password protected, with each user having a different login and password, and it must keep track of user lo gins/outs. Your job as a database developer is to design a database that will serve as the back-end of either a web-based or client-server based application. This database must capture and store all the relevant persistent data that will be used by the application

Keywords:

  • lecturer
  • advising
  • students
  • register
  • courses
  • semester
  • grade
  • application
  • password protected
  • logins/logouts
  • web-based : web server
  • client-server : api service
  • database
  • capture, store
  • relevant persistent data

Entity

  • user ( lecturer )
  • student
  • course
  • student course ( courseid,studentid, semester, grade )

Domain

  • Track of the students whom the lecturer advise
  • Lecturer wants to know which students register for which courses in which semester and what grade they got in those courses
  • The application must be password protected
  • The system must keep track of user logins/logouts

Generating migrations

php artisan make:migration create_users_table # already exists, so need to modified
php artisan make:migration create_students_table
php artisan make:migration create_course_table
php artisan make:migration create_studentcourses_table

Some useful tips

Focus on the heart of software. The heart of software is solving the user's domains.

example.md

Constraint

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateStudentcoursesConstraint extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('studentcourses', function (Blueprint $table) {
            $table->foreign('studentid')
                    ->references('id')
                    ->on('students')
                    ->onDelete('cascade');
            $table->foreign('courseid')
                    ->references('id')
                    ->on('courses')
                    ->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('studentcourses', function (Blueprint $table) {
            $table->dropForeign(['studentid']);
            $table->dropForeign(['courseid']);
        });
    }
}

Headfirst MVC with Laravel

Installation

Install PHP

sudo apt-cache show php
sudo apt-get install software-properties-common -y
sudo apt-get install python-software-properties -y
sudo add-apt-repository ppa:ondrej/php -y
sudo apt-get update && sudo apt-get upgrade -y

Common package

php7.2

sudo apt-get install php7.2 -y

This command will install additional packages:

  • libapache2-mod-php7.2
  • libargon2-0
  • libsodium23
  • libssl1.1
  • php7.2-cli
  • php7.2-common
  • php7.2-json
  • php7.2-opcache
  • php7.2-readline

Common modules

sudo apt-get install php-pear php7.2-curl php7.2-dev php7.2-gd php7.2-mbstring php7.2-zip php7.2-mysql php7.2-xml zip unzip -y

FPM

sudo apt-get install php7.2-fpm -y

Upgrade packages

sudo apt-get upgrade -y

Nginx serve static files

sudo netstat -plnt
sudo apt-get install nginx -y

Apache2 + PHP FPM serve backend

a2dissite 000-default

Create symlink

ubuntu@php:~/shared$ sudo rm /etc/apache2/sites-enabled/000-default.conf
ubuntu@php:~/shared$ sudo ln -s /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-enabled/000-default.conf
sudo cp -rf ~/shared/apache2/* /etc/apache2
sudo systemctl restart apache2
a2ensite php-local
sudo a2enmod actions

PHP FPM Configuration

sudo service php7.2-fpm status
sudo systemctl status php7.2-fpm
a2enmod actions fastcgi alias proxy_fcgi
sudo systemctl restart apache2

Composer

laravel

composer global require laravel/installer
nano ~/.bashrc
export PATH=$HOME/.composer/vendor/bin:$PATH
source ~/.bashrc
laravel new [src] --force

Init project

New app

mkdir headfirst-mvc-laravel
laravel new headfirst-mvc-laravel -f

New composer script

"scripts": {
    "hello": [
        "@php -r \"echo('hello world');\""
    ],
...
composer hello

Generating migrations

php artisan make:migration create_users_table

Dev tools

Visual Code

Ref

Funny things

  1. DB Migration Error
php artisan migrate

database setting is not correct: error 1071: specified key was too long; max key length is 767 bytes

use Illuminate\Support\Facades\Schema;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Schema::defaultStringLength(191);
}

Testing

Dev Notes

1. MYSQL Fundamentals

Ref

Datatypes

2.Migration

php artisan make:migration create_examples_table
@misostack

Copy link
Copy Markdown
Author

Laravel Apache Config

<Directory "C:\workplace\www">
    AllowOverride All
    Options None
    Require all granted
</Directory>

# Default
<VirtualHost *:80>
    DocumentRoot "C:\workplace\www\localhost"
	ServerName localhost
    LogLevel warn
    ErrorLog "C:\workplace\www\logs\localhost_error.log"
    CustomLog "C:\workplace\www\logs\localhost_access.log" combined
</VirtualHost>

# Default
<VirtualHost *:80>
    DocumentRoot "C:\workplace\www\phpguru\hello.phpguru.net\public"
	ServerName hello-phpguru-net.local
    LogLevel warn
    ErrorLog "C:\workplace\www\logs\hello-phpguru-net_error.log"
    CustomLog "C:\workplace\www\logs\hello-phpguru-net_access.log" combined
	
    <Directory "C:\workplace\www\phpguru\hello.phpguru.net">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride All
    Require all granted
    </Directory>
</VirtualHost>

@misostack

Copy link
Copy Markdown
Author

XDebug Config

[xdebug]
zend_extension=xdebug
xdebug.remote_enable=1
xdebug.remote_host=127.0.0.1
xdebug.client_port=9000

Laravel Jetbrain Debug Config

image

PHP JetBrains Config

CLI

image

Version

image

@misostack

Copy link
Copy Markdown
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment