How to call Controller from another folder via url Symfony 2? - php

I have this folder structure (symfony 2 application):
src
|-- Application
| |-- UserBundle
| | |-- Admin
| | |-- Api
| | |-- SomeController.php
| | |-- Controller
| | |-- DefaultController.php
| | |-- DependencyInjection
| | |-- Entity
| | |-- Resources
| | |-- Tests
I want to call some controller from Api folder. How i can do this?
UPD.
I use symfony routing to provide the controller:
application_homepage:
path: /hello/{name}
defaults: { _controller: ApplicationUserBundle:Default:index }
application_some:
path: /api/{slug}
defaults: { _controller: ApplicationUserBundle:../Api/Some:index }
I want to load controller from Api folder

If the namespace of your controller is : Application\UserBundle\Api,
that the className is SomeController and the action is indexAction
You can use this syntax in your routing file :
application_some:
path: /api/{slug}
defaults: { _controller: Application\UserBundle\Api\SomeController::indexAction }

This solution will work too:
application_some:
resource: "#ApplicationUserBundle/Api"
type: annotation
or:
application_some:
resource: "#ApplicationUserBundle/Api/SomeController.php"
type: annotation
see http://symfony.com/doc/current/book/routing.html#including-external-routing-resources

If both controller sit within the same namespace, you could simply do
$controller = new ControllerInApiFolder();
$conotroller->someFunction();
if they do not, then you need to include the controller via use-statement.
use Namespace\My\Controller\Sits\In\ControllerInApiFolder;
If you are trying to access the controller not from another controller, but from a template, an url or a testcase, you should refer to the symfony2 documentation.
Symfony2 Doc
From the edited question above I take it you want to call the controller via the url.
In your browser you can simply type in
path/to/my/web/api/something
or
path/to/my/web/app_dev.php/api/something
where "path/to/my/web" referes to the path of the web folder within your project
EDIT: I think now I am getting the problem...
Symfony2 routing always defaults to the /Controller folder to look for the controller (which is quite nice).
I am not quite sure you should add another folder to this. Instead, the documentation suggests having multiple folders inside the /Controller folder.
If you take a look at the folder structure displayed here: Symfony2 Controller, you will notice that there is an API folder within the /Controller folder.
<your-project>/
├─ ...
└─ src/
└─ AppBundle/
├─ ...
└─ Controller/
├─ DefaultController.php
├─ ...
├─ Api/
│ ├─ ...
│ └─ ...
└─ Backend/
├─ ...
└─ ...

Related

How to override errors templates for Symfony

I want to override the error templates in Symfony 3. I've started by creating a TwigBundle folder withe the personalised twig.
app/
└─ Resources/
└─ TwigBundle/
└─ Exception/
├─ error404.html.twig
├─ error403.html.twig
├─ error.html.twig
Then I checked the routing_dev file which contains this lines :
_errors:
resource: "#TwigBundle/Resources/config/routing/errors.xml"
prefix: /_error
Symfony still displays the default error templates. I want to know if I should verify something else.
PS : I have already checked this documentation : How to Customize Error Pages
EDIT :
The overriden files should reside in a subfolder named views. The correct file tree would look like this:
app/
└─ Resources/
└─ TwigBundle/
└─ views/
└─ Exception/
├─ error404.html.twig
├─ error403.html.twig
├─ error.html.twig
Clear your cache afterwards.
Be aware that those overriden error templates do NOT show up in the dev environment.
If you want to test the templates in your dev environment you must ensure you have imported the following in routing_dev.yml:
_errors:
resource: "#TwigBundle/Resources/config/routing/errors.xml"
prefix: /_error
Now you can access /_error/<error-code>.html (i.e. localhost:8000/_error/404.html with the integrated webserver)

Codeception Gherkin, defining the step implementation path

I’m new to php and codeception and I wanted to use Gherkin with Codeception, and I’ve already setup the bare minimum to make feature files run in Codeception. I now find myself trying to make a scalable structure and make use of the PageObject framework. I created a Steps Folder and I wanted my step implementations kept in that folder. By default running codecept run some.feature loads the class defined in the acceptance.suite.yml file.
Motivation: I want to be able keep my step implementations into it’s own separate folder
Given I have an acceptance.suite.yml file configuration of:
gherkin:
contexts:
default:
- AcceptanceTester
modules:
enabled:
- WebDriver:
url: https://www.google.com/
browser: chrome
- \Helper\Acceptance
And I have a codeception.yml file configuration of:
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
extensions:
enabled:
- Codeception\Extension\RunFailed
And I have my Steps folder under _support:
How do I change the configuration to allow my step implementation to be
called from the Steps folder?
In the gherkin: section of the suite configuration, you need to list your steps classes organised under default:, role: and/or tag: sections. There are example configurations in the official documentation: Gherkin options.
Below is an example from a recent project (using Codeception 2.5.6):
file structure
/app/common
├── codeception.yml
├── tests
│   ├── acceptance.suite.yml
│ ├── _bootstrap.php
│ ├── _data
│ │ └── user.php
│ ├── _support
│ │ ├── AcceptanceTester.php
│ │ ├── Step
│ │ │ └── Acceptance
│ │ │ └── CuratorSteps.php
The layout above for the step class is the default one when generating step object using codecept generate:stepobject command like so:
$ /app/vendor/bin/codecept -c /app/common generate:stepobject acceptance CuratorSteps
acceptance.suite.yml:
# acceptance.suite.yml
namespace: common\tests
suite_namespace: common\tests\acceptance
bootstrap: false
actor: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: http://example.com/
gherkin:
contexts:
default:
- common\tests\AcceptanceTester
role:
curator:
- common\tests\Step\Acceptance\CuratorSteps
The documentation doesn't mention it, but I notice I have to list the full namespace of the step classes, otherwise I'll get "Step definition for ... not found in contexts" errors when running the tests and the gherkin:steps codecept command won't return the step definitions.
output
$ /app/vendor/bin/codecept -vvv -c /app/common gherkin:steps acceptance
Steps from role:curator context:
+--------------------------------------------------------------------+------------------------------------------------------------------------------------------+
| Step | Implementation |
+--------------------------------------------------------------------+------------------------------------------------------------------------------------------+
| I sign in as an admin | common\tests\Step\Acceptance\CuratorSteps::iSignInAsAnAdmin |
| I should see a :arg1 button | common\tests\Step\Acceptance\CuratorSteps::iShouldSeeAButton |
+--------------------------------------------------------------------+------------------------------------------------------------------------------------------+
Steps from default context:
+-------------------------------------+---------------------------------------------------------+
| Step | Implementation |
+-------------------------------------+---------------------------------------------------------+
| I take a screenshot with name :arg1 | common\tests\AcceptanceTester::itakeAScreenshotWithName |
+-------------------------------------+---------------------------------------------------------+

Website cache folder, best location? Inside or outside webroot?

I am curious if there is a best practice concerning the location of a website's cache folder (templates, images, etc)?
I often see it residing in the webroot with index.html/index.php like so,
Server
`-- /var/
`-- www/
-- example.com/
|-- .git/
|-- public_html/
| |-- cache/
| |-- assets/
| `-- index.html
|-- src/
|-- test/
|-- .gitignore
`-- package.json
However, would it be considered bad practice or create any security concerns if I place the cache folder up one level with the rest of the private project files (.git, node_modules, etc)? Like so...
Server
`-- /var/
`-- www/
-- example.com/
|-- .git/
|-- cache/
|-- public_html/
| |-- assets/
| `-- index.html
|-- src/
|-- test/
|-- .gitignore
`-- package.json
Apologies if this isn't a question for here, but I wasn't able to find much information on best practices regarding project folder structure for the cache. The only stuff I could find was about best practices on using the browser cache and other caching tools.
Thanks!

Doctrine - Configure Entities in more than one directory

I'm starting in Doctrine 2.4 and I'm developing a system where I separate the core files and application files, as follows:
/ root
    |-- /Src
         |-- / App
               |-- /Model
                     |- ** (Application Entities) **
         |-- /Core
               |-- /Model
                     |-- ** (Core Entities) **
In the Doctrine documentation shows the following form to set 1 directory for Esntitys:
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__. "/src"), $isDevMode);
But when I have to configure more than one directory that will contain the Entitys of my application, how to proceed?
Thanks in advance!
I found a way to resolve the issue. Simple!!!
Put like this:
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src/App/Model/", __DIR__."/src/Core/Model/"), $isDevMode);

How to develop libraries for symfony while keeping the library code framework independent

I'm creating an app in Symfony which is using a library I'm writing at the same time.
While my application is being built with symfony, I want the component library to be completely framework independent as it needs to be reusable by applications not necessarily written in symfony.
Therefore I have a symfony bundle which integrates the library (the component)
The folder structure is a follows
src/MyVendor/
|-- Bundle
| `-- MyComponentBundle
| |-- Controller
| |-- DependencyInjection
| |-- Resources
| | |-- config
| | `-- views
| | `-- Default
| |-- Tests
| `-- Controller
| `-- MyObjectControllerTest.php
| `-- MyComponentBundle.php
`-- Component
`-- MyComponent
|-- doc
|-- src
`-- MyObject.php
|-- test
`-- MyObjectTest.php
|-- .gitignore
|-- composer.json
|-- LICENSE
|-- README.md
`-- phpunit.xml.dist
Questions:
Is MyComponent directory structure correct as per the PSR-x Autoloading standards? For example, how do I use MyObject from the library inside the bundle, i.e. in MyObjectControllerTest.php
Can MyObject reside in the namespace of MyVendor\MyComponent? If not, how do I have to amend the directory structure so that (1) I can use that namespace, and (2) so that it can be autoloaded inside MyObjectControllerTest.php, i.e. new MyVendor\MyComponent\MyObject(); will work; right now I'm getting PHP Fatal error: Class 'MyVendor\MyComponent\MyObject' not found in ...
Could you please direct me to an online resource to help me to publish MyComponent on github and make it available to symfony (I'm guessing most of that involves just setting up composer.json correctly)
Thank you
It's compatible with PSR-x autoloaders. But your namespaces should be properly registered (PSR-4, PSR-0).
You should create proper autoload to use your component (using composer, for example).
Just create a repo for your component on github, create your composer.json (or copy/past and edit some side one), submit your package on packagist (just insert github project link)

Categories