hocvietcode.com
  • Trang chủ
  • Học lập trình
    • Lập trình C/C++
    • Lập trình HTML
    • Lập trình Javascript
      • Javascript cơ bản
      • ReactJS framework
      • AngularJS framework
      • Typescript cơ bản
      • Angular
    • Lập trình Mobile
      • Lập Trình Dart Cơ Bản
        • Dart Flutter Framework
    • Cơ sở dữ liệu
      • MySQL – MariaDB
      • Micrsoft SQL Server
      • Extensible Markup Language (XML)
      • JSON
    • Lập trình PHP
      • Lập trình PHP cơ bản
      • Laravel Framework
    • Lập trình Java
      • Java Cơ bản
    • Cấu trúc dữ liệu và giải thuật
    • Lập Trình C# Cơ Bản
    • Machine Learning
  • WORDPRESS
    • WordPress cơ bản
    • WordPress nâng cao
    • Chia sẻ WordPress
  • Kiến thức hệ thống
    • Microsoft Azure
    • Docker
    • Linux
  • Chia sẻ IT
    • Tin học văn phòng
      • Microsoft Word
      • Microsoft Excel
    • Marketing
      • Google Adwords
      • Facebook Ads
      • Kiến thức khác
    • Chia sẻ phần mềm
    • Review công nghệ
    • Công cụ – tiện ích
      • Kiểm tra bàn phím online
      • Kiểm tra webcam online
Đăng nhập
  • Đăng nhập / Đăng ký

Please enter key search to display results.

Home
  • Lập trình laravel
Giới thiệu tổng quan về Laravel Framework

Giới thiệu tổng quan về Laravel Framework

  • 20-03-2023
  • Toanngo92
  • 0 Comments

Laravel là một framework PHP mã nguồn mở dành cho người dùng làm việc trong lĩnh vực phát triển ứng dụng Web. Được coi là giải pháp thay thế cho CakePHP và Codelgniter, Laravel sử dụng mô hình Model-View-Controller (MVC) để tạo điều kiện phát triển các ứng dụng Web một cách nhanh chóng và dễ dàng.

Laravel cung cấp cấu trúc cú pháp sạch, phong phú, tính năng có sẵn đa dạng, giúp triển khai nhanh chóng các tính năng CRUD, authentication routing, sessions, caching, sử dụng rộng rãi trong những dự án web thực tế.

Mục lục

  • Vai trò của Laravel Framework
  • Tính năng của Laravel
    • Quy ước về cấu hình
    • Kiến trúc MVC
    • Lệnh thủ công cho tự động hóa (artisan command)
  • Hàm trợ giúp (Helper Functions)
  • Thư viện mở rộng (Extensive Library)
  • Cấu trúc thư mục laravel

Vai trò của Laravel Framework

PHP trước đó được gọi là Personal Home Page (Trang chủ cá nhân) và đã trải qua nhiều lần cập nhật phiên bản để đạt được hình thức hiện tại. Do dễ làm việc, nó là một trong những ngôn ngữ lập trình được hỗ trợ rộng rãi nhất, đặc biệt là bởi cộng đồng mã nguồn mở. Sự hỗ trợ to lớn đã dẫn đến sự sẵn có rộng rãi của các thư viện PHP có sẵn trên Internet dưới dạng mã nguồn mở.

Mặc dù có sẵn nhiều thư viện khác nhau, nhưng điều quan trọng là phải đáp ứng các tiêu chuẩn và hướng dẫn được xác định để viết mã ứng dụng Web, đây là một quá trình tốn nhiều thời gian. Cũng cần nỗ lực để tìm hiểu thông tin chính xác trên Internet, cài đặt các phụ thuộc, định cấu hình từng thư viện và đưa chúng vào các tệp khác nhau. Framework sẽ giúp lập trình viên làm tốt điều này.

Framework là một cấu trúc khuôn mẫu cơ bản được sử dụng để phát triển, thử nghiệm và triển khai các ứng dụng một cách nhanh chóng và hiệu quả. Việc sử dụng framework giúp nhà phát triển tập trung vào vấn đề thực tế và logic ứng dụng, đồng thời cung cấp các công cụ để thực hiện các tác vụ một cách dễ dàng. Nó cũng được sử dụng cho các mục đích sau:

  • Cung cấp sự trừu tượng
  • Để xử lý các nhiệm vụ lặp đi lặp lại
  • Sắp xếp mọi thứ theo một cấu trúc logic

Laravel framework tái sử dụng và lắp ráp các thành phần, phần phụ thuộc và thư viện hiện có để tự động hóa các tác vụ tốn thời gian. Điều này giúp nhà phát triển Web xây dựng các ứng dụng Web theo cách có cấu trúc, dễ dàng viết mới và bảo trì.

Tính năng của Laravel

Laravel bao gồm một API nhất quán và bao gồm một số thành phần để thực hiện các tác vụ phổ biến như tương tác với cơ sở dữ liệu một cách dễ dàng.

Quy ước về cấu hình

Laravel có nhiều quy ước. Nếu các quy ước này được tuân thủ đúng cách, nó có thể dự đoán các phụ thuộc (dependency) và tự động hóa (automate) các tác vụ liên quan đến cấu hình. Tính năng Convention over Configuration của Laravel giúp giảm thời gian và công sức cần thiết để thiết lập và cấu hình các dịch vụ. Những môi trường như vậy phù hợp để Phát triển ứng dụng nhanh.

Có thể thấy một ví dụ đơn giản về Quy ước về Cấu hình trong quá trình sử dụng các mô hình để truy xuất các bản ghi được lưu trữ trong một bảng. Nếu một Model (Mô hình) được tạo với tên Student, thì nó sẽ cố gắng truy xuất các bản ghi từ một tên bảng employees. Nếu tên bảng không phải là tên trường hợp số nhiều của Model, thì tên bảng sẽ phải là được xác định rõ ràng trong mã của Model.

Kiến trúc MVC

Vì Laravel dựa trên kiến trúc Model-View-Controller (MVC), logic lập trình được chia thành các phần sau:

  • Models – Các Model xác định cách dữ liệu được hiển thị và lưu trữ. Các Model là các đối tượng đại diện cho các tài nguyên được sử dụng trong một ứng dụng. Thông thường, chúng được sử dụng để xử lý các bản ghi trong cơ sở dữ liệu. Do đó, các Model có thể được sử dụng để đại diện cho các thực thể khác nhau, chẳng hạn như nhân viên hoặc người dùng hoặc một mặt hàng. Các Model là các đối tượng là các thể hiện của lớp Mô hình cơ sở của Eloquent. Đối tượng này có thể được sử dụng một cách hiệu quả để giữ các bản ghi từ một bảng cơ sở dữ liệu và thực hiện các thao tác trên đó. Các kỹ thuật lập trình ORM Eloquent cho phép các mô hình tương tác nhiều hơn với các bản ghi trong cơ sở dữ liệu.
  • Views – Views xác định cách các trang Web xuất hiện. Họ chịu trách nhiệm tạo các trang Web được trả về dưới dạng phản hồi với sự trợ giúp của bộ điều khiển. Chúng có thể được viết dưới dạng các tệp PHP tiêu chuẩn hoặc có thể được tạo động bằng cách sử dụng ngôn ngữ Tạo khuôn mẫu Blade, ngôn ngữ này cung cấp cách tiếp cận mô-đun để tạo chúng. Blade cho phép tạo các chế độ xem phức tạp từ các bố cục đơn giản hơn.
  • Controllers – Controllers xác định các phản hồi cần thiết cho các yêu cầu khác nhau. Nói một cách đơn giản hơn, các Controller nhận một yêu cầu và sau đó, tùy thuộc vào yêu cầu đó, sẽ tạo ra các phản hồi thích hợp. Tương tự như máy chủ, bộ điều khiển là nơi diễn ra quá trình xử lý yêu cầu thực tế, chẳng hạn như xử lý việc gửi biểu mẫu và tương tác với cơ sở dữ liệu.

Tất cả những thứ này đều có các class mạnh mẽ được cung cấp sẵn rất nhiều phương thức. Các phương thức này thực hiện hầu hết công việc và cung cấp sự trừu tượng hóa từ mã bên dưới. Điều này cho phép nhà phát triển tập trung hơn vào logic nào nên được triển khai hơn là cách thức triển khai nó.

Mô hình MVC

Lệnh thủ công cho tự động hóa (artisan command)

Công cụ artisan được cung cấp sẵn trong Laravel. Nó là một công cụ tự động hóa giúp thiết lập và cấu hình các thành phần khác nhau cho các hoạt động liên quan đến phát triển ứng dụng Web. Nó có thể được sử dụng để tạo các Model, views và controller được điền sẵn mẫu. Nó cũng có thể được sử dụng để tạo máy chủ mẫu, thực hiện các hoạt động cơ sở dữ liệu, v.v.

Để xem danh sách các lệnh artisan:

php artisan list

Output:

Laravel Framework 9.0.0

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display help for the given command. When no command is given display help for the list command
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi|--no-ansi  Force (or disable --no-ansi) ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --env[=ENV]       The environment the command should run under
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  clear-compiled                    Remove the compiled class file
  completion                        Dump the shell completion script
  db                                Start a new database CLI session
  down                              Put the application into maintenance / demo mode
  dump-server                       Start the dump server to collect dump information.
  env                               Display the current framework environment
  help                              Display help for a command
  inspire                           Display an inspiring quote
  list                              List commands
  migrate                           Run the database migrations
  optimize                          Cache the framework bootstrap files
  serve                             Serve the application on the PHP development server
  test                              Run the application tests
  tinker                            Interact with your application
  ui                                Swap the front-end scaffolding for the application
  up                                Bring the application out of maintenance mode
 activitylog
  activitylog:clean                 Clean up old records from the activity log.
 auth
  auth:clear-resets                 Flush expired password reset tokens
 backup
  backup:clean                      Remove all backups older than specified number of days in config.
  backup:list                       Display a list of all backups.
  backup:monitor                    Monitor the health of all backups.
  backup:run                        Run the backup.
 cache
  cache:clear                       Flush the application cache
  cache:forget                      Remove an item from the cache
  cache:table                       Create a migration for the cache database table
 config
  config:cache                      Create a cache file for faster configuration loading
  config:clear                      Remove the configuration cache file
 db
  db:seed                           Seed the database with records
  db:wipe                           Drop all tables, views, and types
 debugbar
  debugbar:clear                    Clear the Debugbar Storage
 event
  event:cache                       Discover and cache the application's events and listeners
  event:clear                       Clear all cached events and listeners
  event:generate                    Generate the missing events and listeners based on registration
  event:list                        List the application's events and listeners
 key
  key:generate                      Set the application key
 log-viewer
  log-viewer:check                  Check all LogViewer requirements.
  log-viewer:clear                  Clear all generated log files
  log-viewer:publish                Publish all LogViewer resources and config files
  log-viewer:stats                  Display stats of all logs.
 make
  make:cast                         Create a new custom Eloquent cast class
  make:channel                      Create a new channel class
  make:chart                        Creates a new chart
  make:command                      Create a new Artisan command
  make:component                    Create a new view component class
  make:controller                   Create a new controller class
  make:event                        Create a new event class
  make:exception                    Create a new custom exception class
  make:factory                      Create a new model factory
  make:job                          Create a new job class
  make:listener                     Create a new event listener class
  make:mail                         Create a new email class
  make:middleware                   Create a new middleware class
  make:migration                    Create a new migration file
  make:model                        Create a new Eloquent model class
  make:notification                 Create a new notification class
  make:observer                     Create a new observer class
  make:policy                       Create a new policy class
  make:provider                     Create a new service provider class
  make:request                      Create a new form request class
  make:resource                     Create a new resource
  make:rule                         Create a new validation rule
  make:scope                        Create a new scope class
  make:seeder                       Create a new seeder class
  make:test                         Create a new test class
  make:view                         Create a new blade template.
 migrate
  migrate:fresh                     Drop all tables and re-run all migrations
  migrate:install                   Create the migration repository
  migrate:refresh                   Reset and re-run all migrations
  migrate:reset                     Rollback all database migrations
  migrate:rollback                  Rollback the last database migration
  migrate:status                    Show the status of each migration
 model
  model:prune                       Prune models that are no longer needed
 module
  module:disable                    Disable the specified module.
  module:dump                       Dump-autoload the specified module or for all module.
  module:enable                     Enable the specified module.
  module:install                    Install the specified module by given package name (vendor/name).
  module:list                       Show list of all modules.
  module:make                       Create a new module.
  module:make-command               Generate new Artisan command for the specified module.
  module:make-controller            Generate new restful controller for the specified module.
  module:make-event                 Create a new event class for the specified module
  module:make-factory               Create a new model factory for the specified module.
  module:make-job                   Create a new job class for the specified module
  module:make-listener              Create a new event listener class for the specified module
  module:make-mail                  Create a new email class for the specified module
  module:make-middleware            Create a new middleware class for the specified module.
  module:make-migration             Create a new migration for the specified module.
  module:make-model                 Create a new model for the specified module.
  module:make-notification          Create a new notification class for the specified module.
  module:make-policy                Create a new policy class for the specified module.
  module:make-provider              Create a new service provider class for the specified module.
  module:make-request               Create a new form request class for the specified module.
  module:make-resource              Create a new resource class for the specified module.
  module:make-rule                  Create a new validation rule for the specified module.
  module:make-seed                  Generate new seeder for the specified module.
  module:make-test                  Create a new test class for the specified module.
  module:migrate                    Migrate the migrations from the specified module or from all modules.
  module:migrate-refresh            Rollback & re-migrate the modules migrations.
  module:migrate-reset              Reset the modules migrations.
  module:migrate-rollback           Rollback the modules migrations.
  module:migrate-status             Status for all module migrations
  module:publish                    Publish a module's assets to the application
  module:publish-config             Publish a module's config files to the application
  module:publish-migration          Publish a module's migrations to the application
  module:publish-translation        Publish a module's translations to the application
  module:route-provider             Create a new route service provider for the specified module.
  module:seed                       Run database seeder from the specified module or from all modules.
  module:setup                      Setting up modules folders for first use.
  module:unuse                      Forget the used module with module:use
  module:update                     Update dependencies for the specified module or for all modules.
  module:use                        Use the specified module.
 notifications
  notifications:table               Create a migration for the notifications table
 optimize
  optimize:clear                    Remove the cached bootstrap files
 package
  package:discover                  Rebuild the cached package manifest
 passport
  passport:client                   Create a client for issuing access tokens
  passport:hash                     Hash all of the existing secrets in the clients table
  passport:install                  Run the commands necessary to prepare Passport for use
  passport:keys                     Create the encryption keys for API authentication
  passport:purge                    Purge revoked and / or expired tokens and authentication codes
 permission
  permission:cache-reset            Reset the permission cache
  permission:create-permission      Create a permission
  permission:create-role            Create a role
  permission:setup-teams            Setup the teams feature by generating the associated migration.
  permission:show                   Show a table of roles and permissions per guard
 pos
  pos:dummyBusiness                 Creates a dummy business in the application
  pos:generateRecurringExpense      Creates recurring expenses if enabled
  pos:generateSubscriptionInvoices  Creates subscribed invoices if enabled
  pos:mapPurchaseSell               Delete existing mapping and Add mapping for purchase & Sell for all transactions of all businesses.
  pos:updateRewardPoints            Checks reward points expiry and updates customer reward points
 queue
  queue:batches-table               Create a migration for the batches database table
  queue:clear                       Delete all of the jobs from the specified queue
  queue:failed                      List all of the failed queue jobs
  queue:failed-table                Create a migration for the failed queue jobs database table
  queue:flush                       Flush all of the failed queue jobs
  queue:forget                      Delete a failed queue job
  queue:listen                      Listen to a given queue
  queue:monitor                     Monitor the size of the specified queues
  queue:prune-batches               Prune stale entries from the batches database
  queue:prune-failed                Prune stale entries from the failed jobs table
  queue:restart                     Restart queue worker daemons after their current job
  queue:retry                       Retry a failed queue job
  queue:retry-batch                 Retry the failed jobs for a batch
  queue:table                       Create a migration for the queue jobs database table
  queue:work                        Start processing jobs on the queue as a daemon
 route
  route:cache                       Create a route cache file for faster route registration
  route:clear                       Remove the route cache file
  route:list                        List all registered routes
 schedule
  schedule:clear-cache              Delete the cached mutex files created by scheduler
  schedule:list                     List the scheduled commands
  schedule:run                      Run the scheduled commands
  schedule:test                     Run a scheduled command
  schedule:work                     Start the schedule worker
 schema
  schema:dump                       Dump the given database schema
 session
  session:table                     Create a migration for the session database table
 storage
  storage:link                      Create the symbolic links configured for the application
 stub
  stub:publish                      Publish all stubs that are available for customization
 twilio
  twilio:call                       Twilio command to test Twilio Call API Integration.
  twilio:sms                        Twilio command to test Twilio SMS API Integration.
 ui
  ui:auth                           Scaffold basic login and registration views and routes
  ui:controllers                    Scaffold the authentication controllers
 vendor
  vendor:publish                    Publish any publishable assets from vendor packages
 view
  view:cache                        Compile all of the application's Blade templates
  view:clear                        Clear all compiled view files

Hàm trợ giúp (Helper Functions)

Có nhiều chức năng thường được sử dụng trong phát triển ứng dụng Web có thể yêu cầu nhà phát triển nhập các thư viện khác nhau. Laravel bao gồm nhiều hàm PHP toàn cục được gọi là Trình trợ giúp giúp giảm thời gian và công sức của nhà phát triển bằng cách xử lý các hoạt động thường được sử dụng.

Bản thân Laravel sử dụng các helper functions (hàm trợ giúp) này trong các class, hàm và thậm chí trong cấu hình của nó. Laravel phân loại những helper function này trong các danh mục sau:

Array và Object: Những Helper functions này có thể được sử dụng để thực hiện các thao tác trên mảng và đối tượng. Một số thao tác bao gồm sắp xếp giá trị (Arr:sort()), lấy giá trị của phần tử đầu tiên (head()) hoặc cuối cùng (last()) và đặt giá trị tại một vị trí (arr:set()).

Paths: Những Helper functions này lưu trữ đường dẫn của các vị trí thường được sử dụng, chẳng hạn như đường dẫn của thư mục tài nguyên (resource_path()), đường dẫn của thư mục ứng dụng (app_path()) và đường dẫn của thư mục cấu hình (config_path()).

String: Những trình trợ giúp này có thể được sử dụng để thực hiện các thao tác trên chuỗi, chẳng hạn như tìm xem chuỗi đó có chứa chuỗi con khác không (Str::contains()) và chuyển đổi chuỗi thành trường hợp lạc đà (Str::camel()).

URL: Những helper functions này có thể được sử dụng để làm sạch hoặc tạo URL, chẳng hạn như làm sạch một url thành một url an toàn.

Các helper functions khác, ví dụ: tìm giá trị của biến môi trường (env()), tìm. ngày hiện tại (today()) và chuyển hướng người dùng đến một url khác (redirect()).

Thư viện mở rộng (Extensive Library)

Các thư viện này sẽ phải được cài đặt và cấu hình thủ công khi tạo Ứng dụng web không có Laravel. Validate là một ví dụ, có thể dễ dàng sử dụng để xác thực người dùng trong khi xử lý các vấn đề bảo mật như xử lý các cuộc tấn công bao gồm SQL Injection, bỏ qua xác thực và giả mạo, mã hóa và ghi nhật ký yêu cầu chéo trang.

Nó cũng cho phép bạn bao gồm các thư viện bên ngoài bằng cách sử dụng trình quản lý composer:

composer require <library-name>

Danh sách các thư viện có sẵn có thể được tìm thấy trên https://packagist.org/

Hướng dẫn cài đặt composer có thể xem bài viết: Cài đặt composer sử dụng cho Xampp trên Windows

Cấu trúc thư mục laravel

Cấu trúc thư mục trong Laravel được chia thành các phần sau:

Thư mục app: Chứa mã nguồn lõi chạy ứng dụng Laravel. Hầu hết, các class được lưu trữ trong thư mục này và các thư mục con của nó.

Thư mục bootstrap: Bao gồm mã đối chiếu tất cả các tệp trải khắp các thư mục và chạy ứng dụng. Thư mục này chứa file app.php sử dụng để khởi động framework, nó cũng chứa một thư mục con là cache chứa các tệp được tạo ra theo framework nhằm tối ưu hiệu suất. Thông thường thư mục này không cần sửa đổi bất kì tệp nào.

Thư mục config: Bao gồm các file liên quan đến cấu hình của các các thành phần ứng dụng.

Thư mục database: thư mục này migrations, seeds, model factories (các tác vụ tự động hóa tạo db, model, dữ liệu mẫu). Nếu muốn, chúng ta cũng có thể sử dụng thư mục này để chứa cơ sở dữ liệu SQLite.

Thư mục public: Bao gồm các tệp phải được hiển thị với Intemet. Do đó, tệp index.php bên trong thư mục public sẽ gọi tệp app.php trong thư mục bootstrap để khởi động ứng dụng bất cứ khi nào nó nhận được yêu cầu. Tất cả các tệp HTML, CSS và Hình ảnh cũng được lưu trữ trong thư mục này.

Thư mục resources: Bao gồm các views sẽ được sử dụng để hiển thị trang Web cuối cùng (giao diện) cũng như các nội dung chưa được biên dịch liên quan đến javascript và CSS.

Thư mục routes: Cung cấp định nghĩa tuyến đường cho ứng dụng sẽ khớp yêu cầu với phản hồi của nó.

Thư mục storage: Bao gồm dữ liệu do Laravel framework tạo ra để lưu trữ và bộ đệm.
Thư mục tests: Chứa thông số kỹ thuật kiểm tra để kiểm tra tự động.
Thư mục vendor: Chứa thông tin về các phụ thuộc của bên thứ ba được nhập bởi trình quản lý phụ thuộc (dependency manager) của composer.

Bài viết liên quan:

Khái niệm và sử dụng Route trong Laravel
Hướng dẫn cài đặt laravel chi tiết qua video
Hướng dẫn xử lý sắp xếp files theo thời gian cho thư viện laravel file manager
Xử lý lỗi “The stream or file “laravel.log” could not be opened in append mode: failed to open stream: Permission denied ” trên các môi trường khác nhau
Cài đặt và bật extension fileinfo trong PHP 7 linux (centos,ubuntu)

THÊM BÌNH LUẬN Cancel reply

Dịch vụ thiết kế Wesbite

NỘI DUNG MỚI CẬP NHẬT

4. KIỂM THỬ VÀ TRIỂN KHAI HỆ THỐNG

2. PHÂN TÍCH VÀ ĐẶC TẢ HỆ THỐNG

3. THIẾT KẾ HỆ THỐNG

1. TỔNG QUAN KIẾN THỨC THỰC HÀNH TRIỂN KHAI DỰ ÁN CÔNG NGHỆ THÔNG TIN

Hướng dẫn tự cài đặt n8n comunity trên CyberPanel, trỏ tên miền

Giới thiệu

hocvietcode.com là website chia sẻ và cập nhật tin tức công nghệ, chia sẻ kiến thức, kỹ năng. Chúng tôi rất cảm ơn và mong muốn nhận được nhiều phản hồi để có thể phục vụ quý bạn đọc tốt hơn !

Liên hệ quảng cáo: [email protected]

Kết nối với HỌC VIẾT CODE

© hocvietcode.com - Tech888 Co .Ltd since 2019

Đăng nhập

Trở thành một phần của cộng đồng của chúng tôi!
Registration complete. Please check your email.
Đăng nhập bằng google
Đăng kýBạn quên mật khẩu?

Create an account

Welcome! Register for an account
The user name or email address is not correct.
Registration confirmation will be emailed to you.
Log in Lost your password?

Reset password

Recover your password
Password reset email has been sent.
The email could not be sent. Possible reason: your host may have disabled the mail function.
A password will be e-mailed to you.
Log in Register
×