laravel 通过 Composer 下载后如何设置 Bootstrap?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19118367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:39:18  来源:igfitidea点击:

How do I set up Bootstrap after downloading via Composer?

laravellaravel-4twitter-bootstrap-3composer-php

提问by Artisan

I'm a beginner with Composer, so I know little about it and have little experience with web application development.

我是 Composer 的初学者,所以我对它知之甚少,对 Web 应用程序开发也没有什么经验。

I was reading the Nettuts+Tutorial, and have a basic question about Composer.

我正在阅读Nettuts+教程,并且有一个关于 Composer 的基本问题。

{
  "require": {
    "laravel/framework": "4.0.*",
    "way/generators": "dev-master",
    "twitter/bootstrap": "dev-master",
    "conarwelsh/mustache-l4": "dev-master"
  },
  "require-dev": {
    "phpunit/phpunit": "3.7.*",
    "mockery/mockery": "0.7.*"
  },
  "autoload": {
    "classmap": [
      "app/commands",
      "app/controllers",
      "app/models",
      "app/database/migrations",
      "app/database/seeds",
      "app/tests/TestCase.php"
    ]
  },
  "scripts": {
    "post-update-cmd": "php artisan optimize"
  },
  "minimum-stability": "dev"
}

If I set up my composer.jsonfile as above, after executing composer install --dev, how do I make Bootstrap available for use in the Laravel project?

如果我composer.json按照上面的方式设置我的文件,在执行之后composer install --dev,我如何使 Bootstrap 可用于 Laravel 项目?

I mean, I can see the Bootstrap package is downloaded to the vendor directory. Before I only used to download Bootstrap from its official website and manually put the files in the public directory of Laravel, but what is the right way to do this here? Can I leave the Bootstrap files where they are, because I want to update the Bootstrap package to its latest version periodically?

我的意思是,我可以看到 Bootstrap 包已下载到供应商目录。之前我只习惯从其官网下载Bootstrap并手动将文件放在Laravel的公共目录中,但在这里这样做的正确方法是什么?我可以将 Bootstrap 文件保留在原处,因为我想定期将 Bootstrap 包更新到最新版本吗?

Thanks.

谢谢。

采纳答案by brainless

We have artisan command to publish the assets(CSS, JS,..). Something like this should work.

我们有 artisan 命令来发布资产(CSS、JS、..)。像这样的事情应该有效。

php artisan asset:publish --path="vendor/twitter/bootstrap/bootstrap/css" bootstrap/css
php artisan asset:publish --path="vendor/twitter/bootstrap/bootstrap/js" bootstrap/js

i am not sure about the path.. But this should work.

我不确定路径..但​​这应该有效。

回答by Antonio Carlos Ribeiro

As the tutorial says, you have to copy it to your public directory:

正如教程所说,您必须将其复制到您的公共目录:

cp vendor/twitter/bootstrap/docs/assets/js/html5shiv.js public/js/html5shiv.js
cp vendor/twitter/bootstrap/docs/assets/js/bootstrap.min.js public/js/bootstrap.min.js

EDIT:

编辑:

You really have copy them, because your assets files should lie in the public folder onlyand Composer is all about putting packages on your vendor's folder, which must not be visibleto the outside world.

你真的有复制它们,因为你的资产文件应该只位于公共文件夹中,而 Composer 就是把包放在你的供应商文件夹中,这对外界是不可见的。

But you can create a Composer post-install-cmd:

但是你可以创建一个 Composer post-install-cmd:

{
    "scripts": {
        "post-update-cmd": "MyVendor\MyClass::postUpdate",
    }
}

And make it copy those files for you every time an update happens. It can be written using PHP, bash or any other language you can run on your host. Docs: http://getcomposer.org/doc/articles/scripts.md.

并让它在每次更新时为您复制这些文件。它可以使用 PHP、bash 或任何其他可以在主机上运行的语言编写。文档:http: //getcomposer.org/doc/articles/scripts.md

回答by Yoga

Just realised that php artisan asset:publish --path="vendor/twbs/bootstrap/dist/" bootstrapor php artisan vendor:publish --path="vendor/twbs/bootstrap/dist/" bootstrapdo not work anymore.

刚刚意识到php artisan asset:publish --path="vendor/twbs/bootstrap/dist/" bootstrapphp artisan vendor:publish --path="vendor/twbs/bootstrap/dist/" bootstrap不再工作了。

What worked for me is editing the composer.json to add the following under scripts, post-update-cmd:

对我有用的是编辑 composer.json 以在scripts,下添加以下内容post-update-cmd

"scripts": {
        "post-update-cmd": [
            "php artisan optimize",
            "mkdir -p public/bootstrap",
            "cp -R vendor/twbs/bootstrap/dist/ public/bootstrap/"
        ]}

回答by Daniel W.

Just symlink the folder like said above by LeviXC:

只需像LeviXC上面所说的那样对文件夹进行符号链接:

{
    "scripts": {
      "post-update-cmd": "ln -sf vendor/twitter/bootstrap/dist/ public/vendor/bootstrap/"
    }
}

Or multiple commands:

或多个命令:

{
    "scripts": {
        "post-update-cmd": [
           "php artisan optimize",
           "ln -sf vendor/twitter/bootstrap/dist/ public/vendor/bootstrap/"
        ]
    },
}

回答by MauricioOtta

This worked better for me

这对我来说效果更好

"scripts": {
    "post-update-cmd": [
        "mkdir -p html/vendor/",
        "ln -sfr vendor/twbs/bootstrap/dist html/vendor/bootstrap"
    ]
},

The "r" flag made the symlink relative, thus pointing to the real folder

“r”标志使符号链接相对,从而指向真正的文件夹

回答by Adam Astley

I am new to Laravel and Bootstrap (and to using them together) and I stumbled across this thread when having the same issue. I created a new Laravel project, then added Bootstrap by running the following command from within the root directory of the Laravel project:

我是 Laravel 和 Bootstrap 的新手(以及将它们一起使用),并且在遇到相同问题时偶然发现了这个线程。我创建了一个新的 Laravel 项目,然后通过在 Laravel 项目的根目录中运行以下命令来添加 Bootstrap:

%composer require twbs/bootstrap

%composer 需要 twbs/bootstrap

Then, in my view file, I included the following code:

然后,在我的视图文件中,我包含了以下代码:

<link href="css/app.css" rel="stylesheet">

It appears that composer (or bootstrap) adds app.css which includes the Bootstrap css files (which are located in the non-public vendor folder) by reference. Adding the reference to app.css worked, and I was able to use Bootstrap components in my view.

看来作曲家(或引导程序)通过引用添加了 app.css,其中包含引导程序 css 文件(位于非公共供应商文件夹中)。添加对 app.css 的引用有效,并且我能够在我的视图中使用 Bootstrap 组件。

回答by Wolfetto

The solution with composer post update script (post-update-cmd) in Windows environment could be:

post-update-cmd在 Windows 环境中使用 composer post update script ( )的解决方案可能是:

{
  "require": {
    "twbs/bootstrap": "4.3.1"
  },
  "scripts": {
    "post-update-cmd": [
      "RMDIR public\assets\bootstrap /S /Q" ,
      "XCOPY /E /I vendor\twbs\bootstrap\dist public\assets\bootstrap"
    ]
  }
}

You will have the bootstrap files inside public\assets\bootstrapfolder ready to be imported in HTML.

您将public\assets\bootstrap准备好以 HTML 格式导入文件夹内的引导程序文件。