How to use vendor bundles resources in Symfony2 - php

I know this is a very basic question and I'm pretty puzzled that I couldn't find a good answer for that.
Let's say I installed twbs/bootstrap package with Composer, so it lands in its proper vendor/twbs/bootstrap/ directory. Now, what do I need to do to make it actually useful, i.e. copy the bootstrap/dist/ folder containing css/js files to the web/ folder?
Is there any (semi)intelligent 'Symfonic' way to handle this or should I a) copy/update the needed files manually, b) setup e.g. a grunt copy task for that?
I'm using Symfony 2.6 on Windows 7 if that matters.

You can only install repositories that are symfony2 bundles. In case of https://github.com/twbs/bootstrap, it isn't.
You'll have to find another bundle for symfony2 doing the same, like this one for instance: https://github.com/braincrafted/bootstrap-bundle
And as explained in their doc like almost if every bundle doc, here is how you install this bundle (the procedure is the same for the majority of them):
Installation
BraincraftedBootstrapBundle should be installed using Composer:
{
"require": {
"braincrafted/bootstrap-bundle": "~2.0"
}
}
part about Bootstrap and jQuery sniped, not directly relevant to hoiw install a bundle, go read it in the doc.
Then add the bundle to your AppKernel.php:
# app/AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Braincrafted\Bundle\BootstrapBundle\BraincraftedBootstrapBundle(),
);
// ...
}
}
BraincraftedBootstrapBundle highly recommends you to use Assetic for
managing assets. If you do use Assetic for managing your assets, you
should now run the dump command.
php app/console assetic:dump

for posterity :
just added in my assetic config under config.yaml:
assetic:
assets:
bootstrap_css:
inputs:
- "%kernel.root_dir%/../vendor/twbs/bootstrap/dist/css/bootstrap.min.css"
filters:
- cssrewrite
output: bundles/twbs/bootstrap.css
just added after in my base.html.twig template, anywhere in the header :
<link rel="stylesheet" href="{{ asset('bundles/twbs/bootstrap.css') }}" media="screen" />
Do the trick,don't know if it's 'state of the art', but it work.
thanks to http://www.tutodidacte.com/symfony2-installer-bootstrap

As always, the official docs hold the answer: http://symfony.com/doc/current/cookbook/bundles/installation.html

Related

Symfony - How to override a vendor component which is not in a bundle

I'm searching for override a file which is in the vendor's directory
but is not a bundle.
I'm working with Symfony 2.7
To be more specific, i'm trying to override a method in this file :
vendor/akeneo/pim-community-dev/src/Pim/Component/Catalog/Updater/ProductUpdater.php
And I'd like to do it in a file like :
src/MyApp/Component/Catalog/Updater/ProductUpdater.php
All the documentation I've found is relied to parts of a Bundle.
So, is it even possible to do that ?
If it is, how to do it ?
There is a way to override a file using composer.
You can add in your composer.json under the autoload->psr-4 key something like this.
"autoload": {
"psr-4": {
"Pim\\Component\\Catalog\\Updater\\": "MyApp/Pim/Component/Catalog/Updater"
},
Where the first part is the namespace of the file you want to override and the second part is the new path.
This can create some issues when you add new packages to composer, so make sure you run composer dump-autoload to recreated the autoload order.
i don't really like this method, but it did save me a couple of times to fix files from bundles without actually forking the bundle, and if nothing else helps this can me used as a last resort.
More about composer autoload here
https://getcomposer.org/doc/01-basic-usage.md#autoloading
Hope this helps,
Alexandru Cosoi

How to use the css and js file from the vendor folder in Symfony 3?

I'd like to involve jQuery Smart Wizard in my application.
I have update the composer.json and then run composer update.
Here I get the related files in the vendor folder as techlab\smartwizard.
How could I link the css and js file from the twig template?
I have tried to use below command to generate link to the resources.
php bin\console assets:install --symlink --relative
It seems that the command only deal with the folder in resources\Public in each Bundle.
I hope I could use some link as below to get the files.
<link rel="stylesheet" href="{{ asset('bundles/XXX/static//styles/XXX.css') }}">
I know I could create CSS and JS folder under web folder and then copy the related files there. I just wonder if I have to do this manually then why I use composer to install it. I could just download the file and put them in the target folder. That's all.
I am not sure if there is some better way to manage the resource.
Thank you very much for your help.
assets:install command works only if it finds directories/files in Resources/public directory.
I see two options here:
1) create a bundle and add the files in the Resources public directory
2) use bower and install this package as a dependency for frontend.
For this, you may create a `.bowerrc` file to set the
target directory which can be `web/bower`.
Composer is used to manage php dependencies, not frontend ones, except somebody made a package to work with composer and to be integrated in framework.
So, you can use:
1) composer -> php related, backend dependencies
2) bower, npm -> frontend dependencies
I use a precompiler with gulp that manages css, images and js files in the project
Try
php bin\console
To
php bin/console
Where in your command line

ServiceNotFoundException in CheckExceptionOnInvalidReferenceBehaviorPass.php line 58

I recently started learning symfony 2.8 and trying to set up my 1st page on local host. However after configuring my bundle based on this tutorial: https://www.youtube.com/watch?v=S9aQxvEAdcE&list=PLp9j56Yo8a6aWJhLyggonvP7b8vIcYn5Q&index=3
i get this error :
ServiceNotFoundException in CheckExceptionOnInvalidReferenceBehaviorPass.php line 58:
The service "doctrine.orm.layout_entity_manager" has a dependency on a non-existent service "doctrine.orm.quote_strategy.layout".
when trying to open my page with adress localhost/st/web/app_dev.php
The reason I am asking this is that couldnt find anything about this particular problem and have no idea what these services is about. If anyone have any clue what I am doing wrong or why this error occurs please share those with me. I am not adding any code because I am not quite sure which files could be the reason of this error. Thanks in advance
EDITED :
I am creating new symfony project using php storm located in C:\wamp64\www\st (enabled symfony plugin and command line tool)
then generating a bundle called MainBundle
I rename DefaultController.php in MainBundle/Controller to MainController.php and change class within it to MainController.
Then rename default folder in app/Resources/views to layout, as well as base.html.twig to layout.html.twig
Then filling a twig files with boostrap template
Then change MainController.php into this :
<?php
namespace MainBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class MainController extends Controller
{
/**
* #Route ("/", name="home")
*/
public function indexAction()
{
return $this->render('MainBundle:Default:home.html.twig');
}
}
then in src/Resource/views/Default change index.html.twig into home.html.twig
src/MainBundle/Resources/config/routing.yml fill like this :
main:
resource: "#MainBundle/Controller/MainController.php"
prefix: /
type: annotation
Adding github project link https://github.com/marras1/st
Ok so I am not quite sure what did the magic, but i found this idea here: Symfony2 post-update-cmd gives "An error occurred when generating the bootstrap file"
to remove the /bin and /vendor folders of your project.Then make a composer install (or php composer.phar install if your didn't install composer). In addition i ran php composer.phar update doctrine/mongodb-odm doctrine/mongodb-odm-bundle and the whole system seems working just fine for now.

Zend Framework 2 load class PHPGangsta_GoogleAuthenticator

I'm trying to load a class that has an underscore in it in ZF2.
This is the project I want to use: https://github.com/PHPGangsta/GoogleAuthenticator
The folder paths look like this:
/module
/Application
/Service
/MyService.php
/vendor/
/PHPGangsta
/GoogleAuthenticator.php
GoogleAuthenticator.php has a class named PHPGangsta_GoogleAuthenticator which I want to use in MyService.php, without having to require any files.
Also, I cannot change files inside PHPGangsta, because the project is submoduled under git.
Can you help configure zf2 class autoload?
Assuming you installed ZF2 using Composer (which is the recommended method), edit your composer.json to add phpgangsta/googleauthenticator to the require section. Then run composer install. That's it. You should then be able to use the library in your application - you don't need to do any additional autoload configuration.

Can I use composer to deploy my Github Repositories into my projects and change some lines of code

I'm a little unexperienced with composer and I'm starting to use Github more and more. I'm a php developer and I an application to serve as a CMS/Backoffice of the websites I develop to my clients.
I'm using and HMVC framework and I wanted to know if its possible o deploy a module (a repository) in a specific folder with composer, and again with composer change some lines of code.
For example, I have this structure:
- app
-- modules
--- users
--- auth
-- index.php
-- configs.php
- public
-- assets
In this example, users and auth are booth a module. Imagine that I have this repository in rafaelmsantos/posts and want to deploy it on the modules folder. That part I guess is simple, or its difficult?
The second part is the following: imagine that in posts repository I have a class file like this:
<?php
// $this->route('posts', 'postsController->action');
class PostsController extends appController{
function action(){
somestuff();
}
}
And when I deploy the module, I want the composer to read this file and detect that comment above class declaration and insert it into config.php that is in same level that modules folder.
Is this possible too?
Let me know some good tutorials for composer or something.
Thanks, in advance.
Composer have post-install-cmd and post-update-cmd events, which must be declared in composer.json file in scripts part.
For example:
{
"scripts": {
"post-update-cmd": "MyVendor\MyClass::postUpdate",
}
}
where 'postUpdate' is a static method, which add routes as you want.
Full info about scripts in composer

Categories