what is laravel elixir
Laravel Elixir is an API that integrates Gulp and provides a simple solution for compiling Less, Sass, CoffeeScript and handling many other daily tasks in Laravel projects, thereby reducing the hassle of writing the above tedious tasks time, effectively improving programming efficiency.
The operating environment of this tutorial: Windows 7 system, Laravel version 8.5, Dell G3 computer.
Laravel aims to make PHP development easy and enjoyable, so starting from Laravel 5, a new API called LaravelElixir is provided. This API integrates Gulp, providing a simple solution for compiling Less, Sass, CoffeeScript in Laravel projects and handling many other daily tasks, thereby reducing the time of writing the above tedious tasks and effectively improving programming efficiency.
Laravel Elixir provides a simple and smooth API that allows you to define basic Gulp tasks in your Laravel application. Elixir supports many common CSS and JavaScript preprocessors and even includes testing tools. Using chain calls, Elixir allows you to define the development process smoothly, for example:
elixir(function(mix) { mix.sass('app.scss') .coffee('app.coffee'); });
If you have ever been confused about getting started with Gulp and compiling resource files, then you will fall in love with Laravel Elixir , but Laravel does not force you to use Elixir, you are free to choose the automated development process tool you like.
Installation and Configuration
#Installing Node
Before you start using Elixir, you must first make sure that your machine Have Node.js installed.
node -v
By default, Laravel Homestead will include everything you need; however, if you are not using Vagrant, then you can simply browse Node’s download page(http://nodejs.org/download/) to install. [Related recommendations: laravel video tutorial]
Gulp
Next, you need to install Gulp globally (http: //gulpjs.com) NPM extension package:
npm install --global gulp
Laravel Elixir
#The last step is to install Elixir! In every newly installed Laravel code, you will find a file named package.json
in the root directory. Think of it like your composer.json
file, except that it defines Node's dependent extension package instead of PHP's. You can use the following command to install dependent extension packages:
npm install
If you are developing on a Windows system or running a VM on a Windows host system, you need to run npm Turn on
--no-bin-links when installing the install
command:
npm install --no-bin-links
Run Elixir
#Elixir is built on top of Gulp, so to run your Elixir tasks, just run the gulp
command on the command line. Adding the --production
flag to the command will tell Elixir to compress your CSS and JavaScript files:
// 运行所有任务... gulp // 运行所有任务并压缩所有 CSS 及 JavaScript... gulp --production
Monitor resource file modifications
Because it is quite inconvenient to run the gulp
command on the command line every time you modify your resource file, so you can use the gulp watch
command. This command will be run from your command line and monitor resource files for any modifications. When modifications occur, a new file will be automatically compiled:
gulp watch
#. js
contains all your Elixir tasks. Elixir tasks can be chained together to define how your resource files should be compiled.
Less
#To compile Less to CSS, you can use the less
method. The less
method will assume that your Less files are saved in the resources/assets/less
folder. By default, this example task will place the compiled CSS in public/css/app.css
:
elixir(function(mix) { mix.less('app.less'); });
You may want to merge multiple Less file to a single CSS file. Similarly, the generated CSS will be placed in public/css/app.css
:
elixir(function(mix) { mix.less([ 'app.less', 'controllers.less' ]); });
If you want to customize the output location of the compiled CSS, you can Pass the second parameter to the less
method:
elixir(function(mix) { mix.less('app.less', 'public/stylesheets'); }); // 指定输出的文件名称... elixir(function(mix) { mix.less('app.less', 'public/stylesheets/style.css'); });
Sass
sass
方法让你能编译 Sass 至 CSS。Sass 文件的默认读取路径是 resources/assets/sass
,你可以使用此方法:
elixir(function(mix) { mix.sass('app.scss'); });
同样的,如同 less
方法,你可以编译多个 Sass 文件至单个的 CSS 文件,甚至可以自定义生成的 CSS 的输出目录:
elixir(function(mix) { mix.sass([ 'app.scss', 'controllers.scss' ], 'public/assets/css'); });
纯 CSS
如果你只是想将一些纯 CSS 样式合并成单个的文件,你可以使用 styles
方法。此方法的默认路径为 resources/assets/css
目录,而生成的 CSS 会被放置于 public/css/all.css
:
elixir(function(mix) { mix.styles([ 'normalize.css', 'main.css' ]); });
当然,你也可以通过传递第二个参数至 styles
方法,将生成的文件输出至指定的位置:
elixir(function(mix) { mix.styles([ 'normalize.css', 'main.css' ], 'public/assets/css'); });
Source Maps
Source maps 在默认情况下是开启的。因此,针对每个被编译的文件,同目录内都会伴随着一个 *.css.map
文件。这个文件能够让你在浏览器调试时,可以追踪编译后的样式选择器至原始的 Sass 或 Less 位置。
如果你不想为你的 CSS 生成 source maps,你可以使用一个简单的配置选项关闭它们:
elixir.config.sourcemaps = false; elixir(function(mix) { mix.sass('app.scss'); });
使用脚本
Elixir 也提供了一些函数来帮助你使用 JavaScript 文件,像是编译 ECMAScript 6、编译 CoffeeScript、Browserify、压缩、及简单的串联纯 JavaScript 文件。
CoffeeScript
coffee
方法可以用于编译 CoffeeScript 至纯 JavaScript。coffee
函数接收一个相对于 resources/assets/coffee
目录的 CoffeeScript 文件名字符串或数组,接着在 public/js
目录生成单个的 app.js
文件:
elixir(function(mix) { mix.coffee(['app.coffee', 'controllers.coffee']); });
Browserify
Elixir 还附带了一个 browserify
方法,给予你在浏览器引入模块及 ECMAScript 6 的有用的特性。
此任务假设你的脚本都保存在 resources/assets/js
,并会将生成的文件放置于 public/js/main.js
:
elixir(function(mix) { mix.browserify('main.js'); });
虽然 Browserify 附带了 Partialify 及 Babelify 转换器,但是只要你愿意,你可以随意安装并增加更多的转换器:
npm install aliasify --save-dev
elixir.config.js.browserify.transformers.push({ name: 'aliasify', options: {} }); elixir(function(mix) { mix.browserify('main.js'); });
Babel
babel
方法可被用于编译 ECMAScript 6 与 7 至纯 JavaScript。此函数接收一个相对于 resources/assets/js
目录的文件数组,接着在 public/js
目录生成单个的 all.js
文件:
elixir(function(mix) { mix.babel([ 'order.js', 'product.js' ]); });
若要选择不同的输出位置,只需简单的指定你希望的路径作为第二个参数。该方法除了 Babel 的编译外,特色与功能同等于 mix.scripts()
。
Scripts
如果你想将多个 JavaScript 文件合并至单个文件,你可以使用 scripts
方法。
scripts
方法假设所有的路径都相对于 resources/assets/js
目录,且默认会将生成的 JavaScript 放置于 public/js/all.js
:
elixir(function(mix) { mix.scripts([ 'jquery.js', 'app.js' ]); });
如果你想多个脚本的集合合并成不同文件,你可以使用调用多个 scripts
方法。给予该方法的第二个参数会为每个串联决定生成的文件名称:
elixir(function(mix) { mix.scripts(['app.js', 'controllers.js'], 'public/js/app.js') .scripts(['forum.js', 'threads.js'], 'public/js/forum.js'); });
如果你想合并指定目录中的所有脚本,你可以使用 scriptsIn
方法。生成的 JavaScript 会被放置在 public/js/all.js
:
elixir(function(mix) { mix.scriptsIn('public/js/some/directory'); });
复制文件与目录
copy
方法可以复制文件与目录至新位置。所有操作路径都相对于项目的根目录:
elixir(function(mix) { mix.copy('vendor/foo/bar.css', 'public/css/bar.css'); }); elixir(function(mix) { mix.copy('vendor/package/views', 'resources/views'); });
版本与缓存清除
许多的开发者会在它们编译后的资源文件中加上时间戳或是唯一的 token,强迫浏览器加载全新的资源文件以取代提供的旧版本代码副本。你可以使用 version
方法让 Elixir 处理它们。
version
方法接收一个相对于 public
目录的文件名称,接着为你的文件名称加上唯一的哈希值,以防止文件被缓存。举例来说,生成出来的文件名称可能像这样:all-16d570a7.css
:
elixir(function(mix) { mix.version('css/all.css'); });
在为文件生成版本之后,你可以在你的 视图 中使用 Laravel 的全局 elixir
PHP 辅助函数来正确加载名称被哈希后的文件。elixir
函数会自动判断被哈希的文件名称:
<link rel="stylesheet" href="{{ elixir('css/all.css') }}">
为多个文件生成版本
你可以传递一个数组至 version
方法来为多个文件生成版本:
elixir(function(mix) { mix.version(['css/all.css', 'js/app.js']); });
一旦该文件被加上版本,你需要使用 elixir
辅助函数来生成哈希文件的正确链接。切记,你只需要传递未哈希文件的名称至 elixir
辅助函数。此函数会自动使用未哈希的名称来判断该文件为目前的哈希版本:
<link rel="stylesheet" href="{{ elixir('css/all.css') }}">
BrowserSync
当你对前端资源进行修改后,BrowserSync 会自动刷新你的网页浏览器。你可以使用 browserSync
方法来告知 Elixir,当你运行 gulp watch
命令时启动 BrowserSync 服务器:
elixir(function(mix) { mix.browserSync(); });
一旦你运行 gulp watch
,就能使用连接端口 3000 启用浏览器同步并访问你的网页应用程序:http://homestead.app:3000
。如果你在本机开发所使用的域名不是 homestead.app
,那么你可以传递一个 选项 的数组作为 browserSync
方法的第一个参数:
elixir(function(mix) { mix.browserSync({ proxy: 'project.app' }); });
调用既有的 Gulp 任务
如果你需要在 Elixir 调用一个既有的 Gulp 任务,你可以使用 task
方法。举个例子,假设你有一个 Gulp 任务,当你调用时会输出一些简单的文本:
gulp.task('speak', function() { var message = 'Tea...Earl Grey...Hot'; gulp.src('').pipe(shell('say ' + message)); });
如果你希望在 Elixir 中调用这个任务,使用 mix.task
方法并传递该任务的名称作为该方法唯一的参数:
elixir(function(mix) { mix.task('speak'); });
自定义监控器
如果你想注册一个监控器让你的自定义任务能在每次文件改变时就运行,只需传递一个正则表达式作为 task
方法的第二个参数:
elixir(function(mix) { mix.task('speak', 'app/**/*.php'); });
编写 Elixir 扩展功能
如果你需要比 Elixir 的 task
方法更灵活的方案,你可以创建自定义的 Elixir 扩展功能。Elixir 扩展功能允许你传递参数至你的自定义任务。举例来说,你可以编写一个扩展功能,像是:
// 文件:elixir-extensions.js var gulp = require('gulp'); var shell = require('gulp-shell'); var Elixir = require('laravel-elixir'); var Task = Elixir.Task; Elixir.extend('speak', function(message) { new Task('speak', function() { return gulp.src('').pipe(shell('say ' + message)); }); }); // mix.speak('Hello World');
就是这样!注意,你的 Gulp 具体的逻辑必须被放置在 Task
第二个参数传递的构造器函数里面。你可以将此扩展功能放置在 Gulpfile 的上方,取而代之也可以导出至一个自定义任务的文件。举个例子,如果你将你的扩展功能放置在 elixir-extensions.js
文件中,那么你可以在 Gulpfile
中像这样引入该文件:
// 文件:Gulpfile.js var elixir = require('laravel-elixir'); require('./elixir-extensions') elixir(function(mix) { mix.speak('Tea, Earl Grey, Hot'); });
自定义监控器
如果你想在运行 gulp watch
时能够重新触发你的自定义任务,你可以注册一个监控器:
new Task('speak', function() { return gulp.src('').pipe(shell('say ' + message)); }) .watch('./app/**');
相关推荐:最新的五个Laravel视频教程
The above is the detailed content of what is laravel elixir. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

Laravel provides a comprehensive Auth framework for implementing user login functions, including: Defining user models (Eloquent model), creating login forms (Blade template engine), writing login controllers (inheriting Auth\LoginController), verifying login requests (Auth::attempt) Redirecting after login is successful (redirect) considering security factors: hash passwords, anti-CSRF protection, rate limiting and security headers. In addition, the Auth framework also provides functions such as resetting passwords, registering and verifying emails. For details, please refer to the Laravel documentation: https://laravel.com/doc

Efficiently process 7 million records and create interactive maps with geospatial technology. This article explores how to efficiently process over 7 million records using Laravel and MySQL and convert them into interactive map visualizations. Initial challenge project requirements: Extract valuable insights using 7 million records in MySQL database. Many people first consider programming languages, but ignore the database itself: Can it meet the needs? Is data migration or structural adjustment required? Can MySQL withstand such a large data load? Preliminary analysis: Key filters and properties need to be identified. After analysis, it was found that only a few attributes were related to the solution. We verified the feasibility of the filter and set some restrictions to optimize the search. Map search based on city

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

How does Laravel play a role in backend logic? It simplifies and enhances backend development through routing systems, EloquentORM, authentication and authorization, event and listeners, and performance optimization. 1. The routing system allows the definition of URL structure and request processing logic. 2.EloquentORM simplifies database interaction. 3. The authentication and authorization system is convenient for user management. 4. The event and listener implement loosely coupled code structure. 5. Performance optimization improves application efficiency through caching and queueing.

Want to learn the Laravel framework, but suffer from no resources or economic pressure? This article provides you with free learning of Laravel, teaching you how to use resources such as online platforms, documents and community forums to lay a solid foundation for your PHP development journey from getting started to master.
