×ðÁú¿­Ê±¹ÙÍøµÇ¼

ÔõÑùʹÓÃLaravelʵÏÖÇ°ºó¶ËÊèÉ¢°²ÅÅ

ÔÚÏÖÏÖÔÚ»¥ÁªÍøÌìÏÂÖУ¬ÒÔwebÓ¦ÓóÌÐò×÷Ϊ½¹µãµÄÈí¼þЧÀÍÔ½À´Ô½Ê¢ÐС£ÆäÖУ¬laravel¿ò¼Ü×÷ΪphpÓïÑÔµÄÒ»¸öÓÅÒ쿪·¢¿ò¼Ü£¬²»µ«¾ßÓиßЧµÄÐÔÄÜ£¬²¢ÇÒ»¹ÓµÓÐÓѺõĿª·¢ÌåÑé¡¢¸»ºñµÄ¿ªÔ´ community¡¢Ç¿Ê¢µÄ orm ºÍǨáãϵͳµÈһϵÁÐÓŵã¡£¶ølaravelÔÚºǫ́¿ª·¢ÖУ¬×î½üÒ²Öð½¥Ê¢ÐÐÆðÁËÇ°ºó¶ËÊèÉ¢µÄ¿ª·¢Ä£Ê½¡£±¾ÆªÎÄÕ½«ÏÈÈÝÔõÑùʹÓà laravel ʵÏÖÇ°ºó¶ËÊèÉ¢°²ÅÅ¡£

Ò»¡¢Ê²Ã´ÊÇÇ°ºó¶ËÊèÉ¢

Ç°ºó¶ËÊèÉ¢ÊÇÒ»ÖÖÐ嵀 Web Ó¦ÓóÌÐò¿ª·¢·½·¨£¬¸Ã·½·¨´ÓÊÖÒÕʵÏÖÉϽ«Ç°¶ËºÍºó¶ËÍêÈ«ÊèÉ¢¡£Ç°¶Ë³ÌÐòÈÏÕæÌìÉú½çÃ棬²¢ÓëЧÀÍÆ÷ͨѶ»ñÈ¡Êý¾Ý£¬ºǫ́³ÌÐòÔòÈÏÕæÓªÒµÂß¼­µÄ´¦ÀíºÍÊý¾Ý¿âµÄ»á¼û¡£

ÕâÖÖ·½·¨ÓÐÐí¶àÀûÒæ¡£Ê×ÏÈ£¬Ëü¿ÉÒÔÌá¸ßÇ°ºó¶Ë¿ª·¢µÄЧÂÊ¡£Ç°¶ËºÍºó¶Ë¿ª·¢Ö°Ô±¿ÉÒÔ²¢Ðпª·¢£¬ïÔÌ­ÁËÏ໥¼äµÄÒÀÀµ £»Æä´Î£¬Ëü¿ÉÒÔÌá¸ßÓ¦ÓóÌÐòµÄÐÔÄÜ¡£ÓÉÓÚÇ°¶ËºÍºó¶ËЧÀÍ¿ÉÒÔ»®·Ö¾ÙÐа²ÅźÍÀ©ÈÝ£¬ÒÔÊÇϵͳµÄÕûÌåÐÔÄÜ¿ÉÒÔ»ñµÃºÜºÃµÄÌáÉý¡£ÁíÍ⣬ÕâÖÖ·½·¨¿ÉÒÔÈÃÇ°¶ËºÍºó¶Ë¿ª·¢Ö°Ô±¶¼¿ÉÒÔ¾¡¿ÉÄܵØרעÓÚ¸÷×ÔµÄÁìÓò£¬Ìá¸ß´úÂëÖÊÁ¿ºÍ¿Éά»¤ÐÔ¡£

¶þ¡¢Laravel Ç°ºó¶ËÊèÉ¢µÄʵÏÖ

ÔÚ Laravel ¿ª·¢ÖУ¬Ç°ºó¶ËÊèÉ¢µÄʵÏÖÐèҪʹÓõ½Ò»Ð©Ç°¶Ë¿ò¼Ü¡£ÆäÖУ¬ÎÒÃÇ¿ÉÒÔʹÓà Vue.js¡¢React »ò Angular µÈÖ÷Á÷¿ò¼ÜÀ´×÷Ϊǰ¶ËµÄ¿ª·¢¼Æ»®¡£ÔÚ Laravel ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÁ½ÖÖ·½·¨À´ÊµÏÖÇ°ºó¶ËµÄÊèÉ¢¡£

½¨ÉèÒ»¸öеÄÇ°¶ËÏîÄ¿

ÎÒÃÇ¿ÉÒÔÏȽ¨ÉèÒ»¸ö×ÔÁ¦µÄÇ°¶ËÏîÄ¿£¬ÔÙÒÔ API µÄ·½·¨Óë Laravel ºó¶Ë¾ÙÐн»»¥¡£ÕâÖÖģʽÏ£¬Laravel Ö»ÈÏÕæºó¶ËÊý¾ÝAPI½Ó¿ÚµÄ±àд£¬Ç°¶ËʹÓà AJAX »ò Fetch API µÄ·½·¨À´ÇëÇóºó¶ËµÄÊý¾Ý½Ó¿Ú¡£Ç°¶ËºÍºó¶ËµÄ´úÂë¿ÉÒÔ»®·Ö¾ÙÐÐÔÚ²î±ðµÄЧÀÍÆ÷»ò¶Ë¿Ú¾ÙÐа²ÅÅ¡£

ÕâÖÖ·½·¨µÄÓŵãÊÇÇ°¶ËºÍºó¶ËµÄÊèÉ¢¶ÈºÜÊǸߣ¬¿ª·¢Ö°Ô±¿ÉÒÔ³ä·ÖÑéÕ¹¸÷×ÔµÄÓÅÊÆ£¬Í¬Ê±Ò²Äܹ»Ìá¸ßÓ¦ÓóÌÐòµÄÐÔÄÜ¡ £»¹¿ÉÒÔÔËÓÃһЩÏÖ´úÇ°¶Ë¿ò¼ÜºÍ¹¤¾ß£¬ÌáÉý¿ª·¢Ð§ÂʺͿª·¢ÌåÑé¡£

ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀýÀ´ÑÝʾÕâÖÖ·½·¨µÄʵÏÖ¡£ÎÒÃÇÒÔ Laravel Ϊºó¶Ë£¬Vue.js Ϊǰ¶ËΪÀý£º

1.1 ½¨ÉèÒ»¸öÐ嵀 Laravel ÏîÄ¿

Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÏÂÁîÐÐÖн¨ÉèÒ»¸öÐ嵀 Laravel ÏîÄ¿£º

composer create-project --prefer-dist laravel/laravel blog

µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ

1.2 ½¨ÉèÒ»¸öÐ嵀 Vue.js ÏîÄ¿

½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÐ嵀 Vue.js ÏîÄ¿£º

npm install -g vue-cli
vue init webpack frontend

µÇ¼ºó¸´ÖÆ

1.3 ÉèÖà Laravel ºÍ Vue.js

½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÉèÖà routes/api.php ÎļþÀ´ÏìÓ¦ Vue.js Ç°¶ËµÄÇëÇó¡£

Route::get('/todos', function () {
    return App\Todo::all();
});

µÇ¼ºó¸´ÖÆ

ÔÚ frontend/src/App.vue ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓà Axios »òÈκÎÆäËû AJAX ¿âÀ´»ñÈ¡ºó¶Ë API¡£ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃǽ«Ê¹Óà Axios ¿â¡£

<template>
  <div>
    <div>
      <input>
      <label>{{ todo.title }}</label>
    </div>
  </div>
</template><script>
import axios from 'axios'

export default {
  data () {
    return {
      todos: []
    }
  },
  created () {
    axios.get('/api/todos')
      .then(response => {
        this.todos = response.data
      })
      .catch(error => {
        console.log(error)
      })
  },
  methods: {
    toggle (todo) {
      todo.completed = !todo.completed
      axios.put('/api/todos/' + todo.id, todo)
        .then(response => {})
        .catch(error => {
          console.log(error)
        })
    }
  }
}
</script>

µÇ¼ºó¸´ÖÆ

ÔÚ frontend/config/index.js ÖУ¬ÎÒÃÇ¿ÉÒÔ½« Vue.js Ç°¶ËÉèÖÃΪʹÓÃÓë Laravel ºó¶Ë²î±ðµÄ¶Ë¿Ú¡£È»ºó£¬ÎÒÃÇ¿ÉÒÔÔËÐв¢»á¼ûÕâ¸öÓ¦ÓóÌÐò¡£

php artisan serve --port=8000
cd frontend
npm start

µÇ¼ºó¸´ÖÆ

ʹÓà Laravel Mix ´ò°üÇ°¶ËÏîÄ¿

ÁíÒ»¸ö·½·¨Êǽ«Ç°¶Ë´úÂëºÍ Laravel ºǫ́´úÂë´ò°üµ½Í³Ò»¸öÏîÄ¿ÖоÙÐа²ÅÅ£¬ÕâÖÖģʽÏ Laravel Mix ×÷Ϊ¹¤¾ßÓÃÓÚ¹¹½¨Ç°¶ËÓ¦ÓóÌÐò¡£Laravel Mix ÊÇÒ»¸ö¼ò»¯ÁË Webpack µÄ¹¹½¨¹¤¾ß£¬¿ÉÒÔÈÃÎÒÃÇÇáËɵشò°üÇ°¶Ë×ÊÔ´¡£

ÕâÖÖ·½·¨µÄÓŵãÊÇÇ°ºó¶Ë´úÂë»á´ò°ü³ÉÒ»¸öÕûÌ壬Àû±ã°²ÅźÍά»¤¡£ÎÒÃÇ¿ÉÒÔʹÓÃÀàËÆÓÚ npm run dev ºÍ npm run build µÄÏÂÁîÀ´±àÒëÇ°¶Ë´úÂ룬²¢½«±àÒëЧ¹û·ÅÔÚ Laravel µÄ public Ŀ¼ÖУ¬ÕâÑùÎÒÃǾͿÉÒÔͨ¹ýä¯ÀÀÆ÷Ö±½Ó»á¼ûÓ¦ÓóÌÐòÁË¡£

ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀýÀ´ÑÝʾÕâÖÖ·½·¨µÄʵÏÖ£º

2.1 ½¨ÉèÒ»¸öÐ嵀 Laravel ÏîÄ¿

Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÏÂÁîÐÐÖн¨ÉèÒ»¸öÐ嵀 Laravel ÏîÄ¿£º

composer create-project --prefer-dist laravel/laravel blog

µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ

2.2 ×°Öà Node.js ºÍ NPM

ÔÚ½ÓÏÂÀ´µÄ°ì·¨ÖУ¬ÎÒÃÇÐèҪװÖà Node.js ºÍ NPM¡£

ÔÚ Ubuntu ÖУ¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî×°Öãº

sudo apt-get install nodejs
sudo apt-get install npm

µÇ¼ºó¸´ÖÆ

2.3 ÔÚ Laravel ÖÐ×°Öà Laravel Mix

È»ºó£¬ÎÒÃÇÐèҪװÖà Laravel Mix£º

npm install --save-dev laravel-mix

µÇ¼ºó¸´ÖÆ

È»ºó£¬ÎÒÃÇÐèÒªÖ´ÐÐÒÔÏÂÏÂÁîÀ´ÌìÉú webpack.mix.js ÉèÖÃÎļþ£º

node_modules/.bin/mix

µÇ¼ºó¸´ÖÆ

2.4 ±àдǰ¶Ë´úÂë

½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª±àдǰ¶Ë´úÂë¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔÔÚ resources/assets/js/app.js ÎļþÖбàдһЩ JavaScript ´úÂë¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʾÀý£º

"use strict";

window.Vue = require('vue');

Vue.component('example-component', require('./components/ExampleComponent.vue'));

const app = new Vue({
    el: '#app'
});

µÇ¼ºó¸´ÖÆ

2.5 ±àдǰ¶Ë×ÊÔ´

ÎÒÃÇ¿ÉÒÔ°ÑÇ°¶ËµÄ×ÊÔ´Îļþ·ÅÔÚ resources/assets Ŀ¼Ï¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔÔÚ resources/assets/sass/app.scss ÖбàдһЩ CSS Ñùʽ¡£

html, body {
  height: 100%;
  margin: 0;
}

#app {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.title {
  font-size: 24px;
  text-align: center;
}

µÇ¼ºó¸´ÖÆ

2.6 ÉèÖÃ Laravel Mix

ÎÒÃÇÐèÒªÔÚ webpack.mix.js ÎļþÖÐÉèÖà Laravel Mix¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓà .sass() ÒªÁìÀ´ÌìÉú CSS Îļþ£¬²¢Ê¹Óà .js() ÒªÁìÀ´ÌìÉú JavaScript Îļþ£º

const mix = require('laravel-mix');

mix.js('resources/assets/js/app.js', 'public/js')
   .sass('resources/assets/sass/app.scss', 'public/css');

µÇ¼ºó¸´ÖÆ

2.7 ±àÒëÇ°¶Ë×ÊÔ´

½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔÔËÐÐÒÔÏÂÏÂÁîÀ´±àÒëÇ°¶Ë×ÊÔ´£º

npm run dev

µÇ¼ºó¸´ÖÆ

»ò

npm run watch

µÇ¼ºó¸´ÖÆ

ÕâÑù£¬ÎÒÃǾͿÉÒÔÔÚä¯ÀÀÆ÷Öп´µ½ÎÒÃǵÄÓ¦ÓóÌÐòÁË¡£

°²ÅÅÓ¦ÓóÌÐò

ÎÞÂÛÎÒÃÇʹÓÃÄÄÖÖ·½·¨À´ÊµÏÖÇ°ºó¶ËÊèÉ¢£¬×îÖÕ¶¼ÐèÒª¾ÙÐа²ÅÅ¡£ÎÒÃÇ¿ÉÒÔʹÓõÚÈý·½¹¤¾ßÈç Jenkins¡¢Capistrano ºÍ Docker Compose µÈÀ´×Ô¶¯»¯°²ÅÅ¡£ÕâÀïÏÈÈÝÒ»ÖÖ»ùÓÚ NGINX + PHP-FPM + MySQL µÄ°²ÅÅ·½·¨¡£

3.1 ×°ÖÃЧÀÍ

Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖà NGINX¡¢PHP-FPM ºÍ MySQL¡£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÔÚ Ubuntu ÖоÙÐÐ×°Öãº

sudo apt-get install nginx
sudo apt-get install mysql-server
sudo apt-get install php-fpm

µÇ¼ºó¸´ÖÆ

3.2 ÉèÖÃ NGINX

½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÉèÖà NGINX¡£ÎÒÃÇ¿ÉÒÔÔÚ /etc/nginx/sites-available Ŀ¼Ï½¨ÉèÒ»¸öеÄÉèÖÃÎļþ¡£ÒÔÏÂÊÇÉèÖÃÎļþµÄʾÀý£º

server {
    listen 80;
    server_name yourdomain.com;

    root /var/www/public;

    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ /\. {
        deny all;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        internal;
    }
}

µÇ¼ºó¸´ÖÆ

ÎÒÃÇÐèÒª½«ÎÒÃǵĴúÂë°²ÅÅÔÚ /var/www/public Ŀ¼ÖС£ÀýÈ磬ÎÒÃÇʹÓÃÇ°Á½ÖÖ·½·¨ÖеĵÚÒ»ÖÖ·½·¨£¬´úÂë´æ·ÅÔÚÁËÒ»¸ö ×ÔÁ¦µÄÇ°¶ËÏîÄ¿ ÖС£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ±àÒëºÃµÄÇ°¶Ë´úÂ븴ÖƵ½ /var/www/public Ŀ¼ÖУº

cp -r /path/to/frontend/dist/* /var/www/public

µÇ¼ºó¸´ÖÆ

3.3 ÉèÖÃ MySQL

½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÉèÖà MySQL¡£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¾ÙÐÐÇå¾²ÉèÖãº

sudo mysql_secure_installation

µÇ¼ºó¸´ÖÆ

È»ºó£¬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸öÐ嵀 MySQL Êý¾Ý¿â£º

CREATE DATABASE dbname CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'dbuser'@'localhost' IDENTIFIED BY 'dbpassword';
GRANT ALL PRIVILEGES ON dbname.* TO 'dbuser'@'localhost';

µÇ¼ºó¸´ÖÆ

ÔÚ Laravel µÄ .env ÉèÖÃÎļþÖУ¬ÎÒÃÇÐèÒª¾ÙÐÐÈçÏÂÊý¾Ý¿âÉèÖãº

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=dbname
DB_USERNAME=dbuser
DB_PASSWORD=dbpassword

µÇ¼ºó¸´ÖÆ

3.4 Ö´ÐÐÊý¾Ý¿âǨáã

½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÖ´ÐÐ Laravel Êý¾Ý¿âǨá㣬²¢¾ÙÐÐһЩ³õʼ»¯²Ù×÷£º

php artisan migrate
php artisan db:seed
php artisan key:generate

µÇ¼ºó¸´ÖÆ

3.5 ÖØÆôЧÀÍ

×îºó£¬ÎÒÃÇÐèÒªÖØÆô NGINX ºÍ PHP-FPM ЧÀÍ£¬Ê¹ÉèÖÃÉúЧ£º

sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm

µÇ¼ºó¸´ÖÆ

ÖÁ´Ë£¬ÎÒÃÇ¿ÉÒÔͨ¹ýä¯ÀÀÆ÷»á¼ûÎÒÃǵÄÓ¦ÓóÌÐò£¬Laravel Ç°ºó¶ËÊèÉ¢°²ÅžÍÍê³ÉÁË¡£

Èý¡¢½áÂÛ

±¾ÎÄÏÈÈÝÁËʹÓà Laravel ʵÏÖÇ°ºó¶ËÊèÉ¢°²ÅŵÄÁ½ÖÖ·½·¨£º½¨ÉèÒ»¸öеÄÇ°¶ËÏîÄ¿ºÍʹÓà Laravel Mix ´ò°üÇ°¶ËÏîÄ¿Á½ÖÖ·½·¨¡£ËäÈ»£¬¹ØÓÚÇ°¶Ë¿ª·¢Ö°Ô±À´Ëµ£¬Ò²¿ÉÒÔÑ¡Ôñ×Ô¼ºÊìϤµÄ¿ò¼Ü¡¢±à³ÌÓïÑÔÀ´¾ÙÐÐÇ°¶Ë¿ª·¢£¬Ö»ÐèÒª×ñÕÕÇ°ºó¶ËÊèÉ¢µÄÔ­Ôò¼´¿É¡£×ÜÖ®£¬Laravel µÄÎÞаÐÔʹµÃËü¿ÉÒÔÓëÐí¶àÏÖ´úÇ°¶Ë¿ò¼ÜºÍ¹¤¾ßÅäºÏʹÓã¬ÉÁ¿ª·¢Ö°Ô±¿ÉÒÔ¸ü×ÔÓɵØÑ¡ÔñÊʺÏ×Ô¼ºµÄ¿ª·¢·½·¨¡£

ÒÔÉϾÍÊÇÔõÑùʹÓÃLaravelʵÏÖÇ°ºó¶ËÊèÉ¢°²ÅŵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ