How do I set up Bootstrap after downloading via Composer? - laravel

I'm a beginner with Composer, so I know little about it and have little experience with web application development.
I was reading the Nettuts+ Tutorial, and have a basic question about 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.json file as above, after executing composer install --dev, how do I make Bootstrap available for use in the Laravel project?
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?
Thanks.

We have artisan command to publish the assets(CSS, JS,..). Something like this should work.
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.

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 only and Composer is all about putting packages on your vendor's folder, which must not be visible to the outside world.
But you can create a 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.

Just realised that php artisan asset:publish --path="vendor/twbs/bootstrap/dist/" bootstrapor php artisan vendor:publish --path="vendor/twbs/bootstrap/dist/" bootstrap do not work anymore.
What worked for me is editing the composer.json to add the following under scripts, post-update-cmd:
"scripts": {
"post-update-cmd": [
"php artisan optimize",
"mkdir -p public/bootstrap",
"cp -R vendor/twbs/bootstrap/dist/ public/bootstrap/"
]}

Just symlink the folder like said above by 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/"
]
},
}

The solution with composer post update script (post-update-cmd) in Windows environment could be:
{
"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\bootstrap folder ready to be imported in HTML.

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

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:
%composer require 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.

Related

Nova Tool not updating

I'm making a Laravel Nova app. I'm trying to create a Nova Tool to import users.
The tool creates just fine, however when i update the code it does not show.
I've digged a bit into this, and the problem seems to be that the tool in the Vendor folder does not get updated.
When i do npm run dev or npm run prod, the tool files get updated inside the /nova-components/{componentname} folder, and not in the vendor folder, which is getting loaded by Nova.
I'm using Xampp on windows.
Inside my nova service provider:
/**
* Get the tools that should be listed in the Nova sidebar.
*
* #return array
*/
public function tools()
{
return [
new UserImport()
];
}
My composer file:
"require": {
"Vrumona/UserImport": "*"
},
"repositories": [
{
"type": "path",
"url": "./nova"
},
{
"type": "path",
"url": "./nova-components/UserImport"
}
],
How do make sure the Tool gets updated in the composer vendor folder?
I can delete the vendor folder and run composer install, but this is a bit tedious while developping.
Thanks!
It is likely that composer is not symlinking the package but rather mirroring. You can confirm by seeing what the output is when you run composer update -- if you see Mirrored from ... then symlinks are unavailable which will cause the issue that you're seeing.
As you noticed, you can force symlinks in the composer file using:
"options": {
"symlink": true // Will force symlinks
}
And the relevant documentation if needed: https://getcomposer.org/doc/05-repositories.md#path
php artisan cache:clear
php artisan route:clear
php artisan config:clear
php artisan view:clear

Composer update not installing the entirety of Laravel app

On deploying my application through Laravel Forge, I'm presented with the following errors:
Fatal error: Class 'Illuminate\Support\Arr' not found in
/home/forge/toono.co/vendor/laravel/framework/src/Illuminate/Support/helpers.php
on line 151
This is after the composer update command has ran. I have SSH'd into the directory and low and behold, the file Arr.php doesn't exist.
The code is pulled from the master branch in BitBucket, and then the following lines are executed on the production server:
cd /home/forge/default
git pull origin master
composer install
php artisan migrate --force
Composer.json:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"laravel/framework": "4.2.*"
},
"require-dev": {
"way/generators": "2.*",
"fzaninotto/faker": "1.4.*#dev",
"barryvdh/laravel-debugbar": "1.*",
"flynsarmy/csv-seeder": "1.0.*"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan debugbar:publish",
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "stable"
}
Composer.lock:
"require": {
"codesleeve/stapler": "~1",
"laravel/framework": "~4",
"php": ">=5.4.0"
}
Why has composer update missed that file? After a local update and even on the master branch in Github, Arr.php is there?
If I am missing any necessary code that you require, please let me know.
Any help would be greatly appreciated.
I solved this by deleting composer.lock and the vendor folder within the production server. I then used composer update.
I solved this by ssh'ing into the box via commandline (ssh forge#ip.of.server)
if you don't know how to ssh into the server, you go to forge -> the server -> SSH Keys.
There you add your public key (if you have not setup ssh keys follow this guide: https://help.github.com/articles/generating-ssh-keys).
If you have ssh keys setup, you simply open terminal and paste the following command and run it; "pbcopy < ~/.ssh/id_rsa.pub", this will copy your public key to your clipboard and you can simply paste that into the ssh key field in forge.
Now i can ssh into the server.
I then navigated to the default folder (cd /home/forge/default) and ran "composer update"
It updates everything and it worked after that.

Composer not downloading src directory for packages

I'm using Laravel and Composer to build a web app.
On my local machine I have its requirements setup within the composer.json file and everything works as it should.
I'm using Github to push to the production server, however, I then run composer install and add the providers and aliases for one of my packages, I then get an error saying that the service provider hasn't been found.
I then proceeded to check the vendor folder, which had the directory in it for the package, but within this directory it's missing all the files/folders, such as the composer.json, src directory, etc.
Any ideas why these are not being downloaded? The src directory for other packages are fine, like symfony, laravel, but not the packages I've set as required.
The package I'm using is artdarek/oauth-4-laravel, on my local machine it downloaded fine including the src directory, etc.
Oh and I'm using Forge by Laravel to deploy to a cloud server at Digital Ocean.
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"laravel/framework": "4.2.*",
"artdarek/oauth-4-laravel": "dev-master"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-4": {
"Karl\\": "app/Karl"
}
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "stable"
}
This might work. Try updating composer (to keep current) and updating your app:
composer self-update
composer install
composer update
I had a slightly similar issue that install didn't resolve but update did

Problems installing dependencies by Composer

When I generate the command "php composer.phar install" I get the following error:
{"error":{"type":"PDOException","message":"SQLSTATE[42000] [1044] Access denied for user ''#'localhost' to database 'database'","file":"/var/www/wingtech/wingadmin/public_html/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php","line":47}}Script php artisan optimize handling the post-install-cmd event returned with an error
http://oi42.tinypic.com/20uc2ma.jpg
Curiously, when I set up a database in app/config/database.php before running the install of the composer this error does not appear
To ease my php version is 5.4.17
And my composer.json is:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"laravel/framework": "4.0.*",
"orchestra/asset": "2.0.*"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/library",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan optimize"
],
"pre-update-cmd": [
"php artisan clear-compiled"
],
"post-update-cmd": [
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "dev"
}
Does anyone know what could be?
You can run Composer without it running Laravel scripts using the following switch:
php composer.phar install --no-scripts
Similar behavior (artisan optimize returning an error) happened when running on php < 5.3 or with multiple php versions and needing to specify the php version in the command line, ie php542 composer.phar install - similar issue here.
But the PDO exception is weird, could you check the scripts part of composer.json ? Is there anything more than the default stuff there?
Do you have an .env file? This is a typical error of the artisan script when the .env file is missing.

Created package in Laravel workbench, but how to transfer to vendor folder?

Let's say my package in Laravel is test/test.
I created the package in the workbench and it's been working great following Jason Lewis' tutorial. Now I want to move the package out of
the workbench to the vendor directory. This is where all tutorials fall short, even the laravel docs. I didn't want to use git to move the files, so I simply copied the test/test package from the workbench to the vendor directory (and then deleted it from the workbench). I didn't copy the test/test/vendor folder from the workbench (or any other files I noticed in the .gitignore file). I then ran composer install from my new vendor/test/test directory. I then did a composer dump-autoload from the laravel root directory.
Now when I run my application I get an error that I did not get when the package was in the workbench:
Class 'Test\Test\TestServiceProvider' not found
(this is coming from \bootstrap\compiled.php on line 4121)
I also did a php artisan dump-autoload from the laravel root and I get this same error.
Any ideas? Or can someone lead me to a tutorial that takes the package development all the way to it's final resting point in the vendor directory?
Got it working.
I added:
"psr-0": {
"Test\\Test": "vendor/test/test/src/"
}
to the autoload section in composer.json in the laravel root directory so it looks like this:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-0": {
"Test\\Test": "vendor/test/test/src/"
}
},
If I decide to put the package on Packagist later then I might remove this from the autoload and just keep the package referenced in the "require" part of my composer.json. We'll see what happens when I get that far!
I think you can install your packages from your hard drive as from local repository like this:
"repositories": [
{
"type":"vcs",
"url":"/path/to/repo/here"
}
],
"require":{
"me/myrepo":"dev-master"
}

Resources