In my symfony 2 project I have a bundle at
src/Cinergy/Bundle/PeopleServiceBundle
Now I'd like to generate a CRUD controller based on a doctrine entity, but I'm constantly failing to enter the correct string for the entity parameter.
I tried things like:
php app/console generate:doctrine:crud --entity=Cinergy/Bundle/PeopleServiceBundle:Group
or
php app/console generate:doctrine:crud --entity=#PeopleServiceBundle:Group
All of them return erros like:
[Doctrine\ORM\ORMException]
Unknown Entity namespace alias '#PeopleServiceBundle'.
What's the right syntax for the --entity parameter? Or is there something missing after all?
This is how the directory structure looks right now:
src/Cinergy/Bundle/PeopleServiceBundle/
├── Controller
│ ├── GroupController.php
│ └── PersonController.php
├── DependencyInjection
│ ├── Configuration.php
│ └── PeopleServiceExtension.php
├── PeopleServiceBundle.php
├── Resources
│ ├── config
│ │ ├── routing.yml
│ │ └── services.yml
│ ├── doc
│ │ └── index.rst
│ ├── public
│ │ ├── css
│ │ ├── images
│ │ └── js
│ ├── translations
│ │ └── messages.fr.xliff
│ └── views
│ └── Default
│ └── index.html.twig
└── Tests
└── Controller
├── GroupControllerTest.php
└── PersonControllerTest.php
After all it turned out that I have to create the entity before I can create the CRUD controller for it. Of course that makes sense. Unfortunately the Sensio Generator Bundle documentation does list the operations in the oposite order which pushed me into the wrong direction.
This means the correct order ist
Generating a New Bundle Skeleton
Generating a New Doctrine Entity Stub
Generating a CRUD Controller Based on a Doctrine Entity
First you need to register your bundle into your AppKernel.
Then simply run the following command.
Don't put # before the bundle's name
php app/console generate:doctrine:crud --entity=PeopleServiceBundle:Group
More about generating a CRUD controller based on a Doctrine entity.
According to the symfony docs, you have to use " The entity name given as a shortcut notation containing the bundle name in which the entity is located and the name of tvhe entity", so it should be something like
--entity=CinergyPeopleServiceBundle:Group
If you have more than one Bundle and want to use different database connection just update your config.yml and parameters.yml by adding configuration and parameters.
This will solve problem with CRUD generation.
I searched for hours until I found out that in my app/config/config.yml under doctrine.orm I removed auto_mapping: true which caused the issue. This may be useful for other people :)
If it still relevant for someone :)
guys, it's because DoctrineBundle DoctrineExtension compile the list of valid aliases based on all registered bundles, that have 'Entity' (or other configured) folder in them.
So in order to use doctrine:generate:crud or generate:doctrine:crud,
you have to create just folder Entity in your bundle, and not required to create entity first (as command says - it's true).
So you it will work if you already have some entity in your bundle,
or if you have just empty Entity folder in your bundle.
Related
I have a typo3 extension (created with extension manager) and it seems no matter what I try I always get the following error:
Class CM\Parser\Controller\ParserController does not exist. Reflection failed.
I used the explanations for this problem TYPO3 tutorial extension, controller does not exist and "Controller does not exist. Reflection failed." TYPO3. Neither of them seem to work.
My composer.json in the root directory has the following entry:
"autoload": {
"psr-4": {
"CM\\parser\\": "./packages/cm-parser/Classes"
}
}
My typo3conf/ext folder has a symlink on packages/cm-parser. My composer.json inside the extension directory (packages/cm-parser) has the entry:
"autoload": {
"psr-4": {
"CM\\parser\\": "./Classes"
}
}
Thanks in advance for any help.
My directory structure looks like this (starting in /opt/lampp/htdocs/my-new-project) which is a typo3 v9.5 installation
> .
├── packages
│ └── cm-parser
│ ├── Classes
│ ├── Configuration
│ ├── Documentation.tmpl
│ ├── Resources
│ └── Tests
├── public
│ ├── fileadmin
│ │ ├── _processed_
│ │ ├── _temp_
│ │ └── user_upload
│ ├── typo3
│ │ └── sysext
│ ├── typo3conf
│ │ ├── ext
│ │ └── l10n
│ ├── typo3temp
│ │ ├── assets
│ │ └── var
│ └── uploads
│ └── tx_extensionbuilder
├── var
...
In my typo3conf/ext directory there is a symlink called parser to packages/cm-parser (I think the composer created that for me).
So I hope this symlink works for Typo3.
The files ext_emconf.php and ext_localconf.php are also in the right place. The folder structure above only displays my folders (tree -L 3) up to the third level.
The controller class is CM\Parser\Controller\ParserController, while in your composer.json you're using CM\\parser\\ (with a lowercase p) in the PSR4 autoload. This should be CM\\Parser\\
After changing this you need to of course run composer dumpautoload to reload the autoload information.
In your root composer.json file:
➊ You do not need the PSR-4 autoload section for "CM\\parser\\".
➋ You possibly have to add the path to packages/* as a repository.
➌ You have to include the composer namespace of your extension.
In your file system:
➍ You do not need typo3conf/ext/ as a symbolic link to packages/.
Try the following changes:
In your root composer.json file, remove the PSR-4 autoload section as outlined above. Add the packages/ directory as a path under repositories. For example:
{
"repositories": [
{
"type": "composer",
"url": "https://composer.typo3.org/"
},
{
"type": "path",
"url": "packages/*"
}
],
...
}
Store your extension code in the following path: packages/parser/.
Assuming your extension key reads parser and your vendor name is CM, the composer namespace becomes cm/parser. Add this as a requirement to the composer config file. You can use the following command on the command line:
composer require cm/parser:dev-master
This assumes, that packages/parser/ is a valid Git repository and has the master branch (do not use a version in the extension's composer.json file).
If the local Git repository and version (in the example above: dev-master) can be found, composer will automatically install all dependencies as required and it will create a symbolic link:
typo3conf/ext/parser -> ../../../packages/parser/
Also double check if all PHP files show the correct PHP namespace: CM\Parser\... and your controller class name reads ParserController.
If you can share your TYPO3 extension code, upload it to GitHub (or any other place) and share the link here. This way people can review your code and possibly spot further errors.
I understand how and why to structure a Symfony project. I just wonder if is there any preferred structure under the /src direcotry. Beside the Controller, DependencyInjection, Entity etc. how do you organize your source code?
Just crowd everything under Model? Group larger logical bulks into dirs like Service, Model, Job etc? Some group their files by functionality like:
Order, Product, PopupManager etc. What is the most useful?
There are many answers to this question. I think it highly depends on what your application involves. The following are some suggestion but keep in mind that you need to imagine the structure of your application by yourself.
Little project (less than 6months and a not too much of maintenance):
.
├── Controller
├── Entity
├── Factory
├── Provider
├── Repository
├── Security
└── YourCustomThing
Or if you prefer a domain approach (I do) you can have more something like this:
.
├── Product
│ ├── DTO
│ └── Model
└── User
├── Model
├── Provider
└── Security
If you have a more complex application, then you should probably learn about DDD or CQRS. Here is a little DDD example inspired by the DDD Cargo Sample (but there is a lot more to tell about DDD apps, as well as CQRS ones).
.
├── Application
│ ├── Booking
│ │ └── Dto
│ └── Exception
├── Http
│ └── Action
├── Infrastructure
│ ├── Persistence
│ └── Product
│ ├── ActionFactory
│ └── BookingFactory
└── Model
└── Shop
Hope it helps.
I'm using Spring Boot and I want to know how exactly we mention the path to static content in my JSP files?
I tried to make them in src/main/resources/static/css but it was not working, and in my JSP I called them by using:
<link href="<c:url value="/css/bootstrap.min.css" />" rel="stylesheet" type="text/css">
I have no special configuration in my SpringBoot Class just the call SpringApplication.run(...)
Thank you so much for the help!
you have to have configuration that extends WebMvcAutoConfigurationAdapter , it has registry implementation that has automatically scans for some default locations and adds them to classpath
/META-INF/resources/
/resources/
/static/
/public/
Just add,
#Configuration
#EnableWebMvc
#ComponentScan
public class ServerConfiguration extends WebMvcAutoConfiguration{
}
using springboot 1.5.6.RELEASE my folder structure looks like
~/repos/static-content-example/src > tree
.
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── demo
│ │ ├── DemoApplication.java
│ │ └── MvcConfig.java
│ └── resources
│ ├── application.properties
│ ├── public
│ │ └── test.html
│ └── templates
└── test
└── java
└── com
└── example
└── demo
└── DemoApplicationTests.java
and when I start the server, I can browse to
http://localhost:8080/test.html
http://localhost:8080/public/test.html
anything in the folder "public" is accessible by default at your context root (#1 above). MvcConfig.java allows for #2. I always setup that alias so I can ignore security on any URL that starts with /public. In order to do that without the MvcConfig setup, you'd have to put a folder named public inside the public folder, which is just confusing.
I have no idea why spring doesn't do that by default....seems like it would clear up lots of confusion...
I am using Sprockets with Sinatra, as suggested in Sinatra's page docs, but I can't make it work.
When I go to localhost:4567, the page loads correctly but with no styles. If I go to localhost:4567/assets/app.css, I get a not found error. I wonder what I am missing or what is wrong in the way I am using Sprockets?
This is my folder structure:
├── assets
│ ├── css
│ │ ├── app.css
│ │ ├── base.css
│ │ └── normalize.css
├── bin
│ └── app
├── lib
│ ├── app_assets.rb
│ └── main.rb
├── spec
│ ├── spec_helper.rb
│ └── main_spec.rb
├── views
│ └── index.erb
├── Gemfile
├── Gemfile.lock
├── Rakefile
├── .rspec
└── .ruby-version
The contents of app.css are:
//= require normalize
//= require base
The contents of app_assets.rb are:
module AppAssets
def self.environment root_path
environment = Sprockets::Environment.new root_path
environment.append_path './assets/css/'
environment
# get assets
get '/assets/*' do
env['PATH_INFO'].sub!('/assets', '')
settings.environment.call(env)
end
end
end
The contents of lib/main.rb are:
require 'sinatra'
require 'sprockets'
require 'app_assets'
class Main < Sinatra::Base
set :views, "#{settings.root}/../views"
get '/' do
erb :index
end
end
The file views/index.erb contains the line:
<link rel="stylesheet" href="assets/app.css">
And the contents of bin/app are:
#!/usr/bin/env ruby
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'sinatra'
require 'sprockets'
require 'app_assets'
require 'main'
Main.run!
Which I run typing:
$ bin/app
Any help would be appreciated, I'm sure I made something wrong but I can't see what. Can anybody spot it?
The app_assets.rb file is the problem here. When you require this file inside another file, the methods you define inside this module are not automatically included. You need to explicitly include AppAssets wherever you need the self.environment method to exist.
The second issue here is that self.environment is not equivalent to settings.environment. If I understand correctly, what you're trying to do is define the asset routing whenever the module gets included. To achieve this one way is to use the included hook for modules. This hook gets run every time you include a module inside a context. If you use that, the code in app_assets.rb turns to:
module AppAssets
def self.included(klass)
environment = Sprockets::Environment.new klass.settings.root
# note the change to path. Since the file where this gets included
# is inside a sub-folder, we need to traverse to one level above.
environment.append_path '../assets/css/'
klass.set :environment, environment
klass.get '/assets/*' do
env['PATH_INFO'].sub!('/assets', '')
klass.settings.environment.call(env)
end
end
end
The klass argument to this hook is the class into which this module is included. In our case this is the Sinatra class you've described in main.rb. That file looks like:
class Main < Sinatra::Base
include AppAssets
# Same as what you have
end
There's a Sinatra Recipes article about using Sprockets with Sinatra: http://recipes.sinatrarb.com/p/asset_management/sprockets?#article
I read this article by a fellow stackoverflow member Daniel (hello if you're reading this!) on decoupling Symfony bundles which I found really interesting. I'm now in a position where I am about to start a new project from scratch and have been planning on following Daniel's guidance. However, it was written 2.5 years ago, and Symfony's latest releases haven't adopted the structure and neither is there any mention if it under Symfony's Best Practices article. Therefore, I'm wondering if there's a good reason Symfony themselves have not adopted the structure?
Without reiterating Daniel's whole article here, the directory structure he proposes is as follows (along with not using annotations for entities):
src/
└── Vendor/
└── Product/
└── Bundle
└── BlogBundle/
└── ForumBundle/
└── SiteBundle/
└── Controller/
└── IndexController.php
└── Resources/
└── views/
└── index.html.twig
└── ProductSiteBundle.php
└── Entity
└── User.php
└── Repository
└── UserRepository.php
└── Service
└── UserPasswordRetrievalService.php