I want to use symfony routing as standalone routing for my project, I have the following structure:
app
|--project
|----controller
|------catalog
|--------category.php
|--config
|----routes.yml
|--bootstrap
|----application.php
inside category.php I have a class named Category, that has a method named index().
The router is called from boostrap\application.php
How would can I specify the path to the category file in the routes.yml?
Right now it look like below
search:
path: /search
defaults: { _controller: 'app\project\controller\catalog\category\category::index' }
Loading files is not the responsibility of the routing component. This component is just used to match routes to functions inside a class.
If you want classes to be found, you can use two methods:
add an autoloader which loads the specified classes
Follow PSR-0 (with namespaces matching the filenames and directory structure) and use composer's classloader.
I'd suggest to use the second way. This is the standard in PHP, and you're probably already using the autoloader if you fetched the routing component trough composer.
Related
I am trying to integrate an existing library into Laravel5 which itself is not namespaced and uses its own classes in subfolders using require.
I have placed it at 'app/API/libname/mainlibclass.php'.
with sibling directory at 'app/API/libname/toolkit' which contains the classes the library uses.
Calling from a Laravel controller I am unable to create the class using a require statement (correct?) before
$objectinstance=new Mainlibclass();
so in the main Laravel app I have
use app/API/libname/Mainlibclass
then later the usual
$objectinstance=new Mainlibclass();
In the existing library and each of its own used classes I set
namespace app/API/libname
and 'use' where needed.
I now have no class not found but one of the files uses 'implements Iterator' - I am getting error Interface 'App\API\libname\Iterator' not found.
Try adding \ in front of that so it looks like this:
class ABC implements \Iterator {
Edit:
I think it would be better practice to keep external non-psr-4/0 libraries untouched (for easier update if needed in future) and outside of app/ directory.
You could use composer classmap autoload feature for this.
I want doctrine yaml mapping file to point to an external class.
My dir structure looks like this:
src/Core
MyExternalClass.php
Subdir/MyOtherClass.php
src/SomeBundle/Resources/config/doctrine
MyExternalClass.orm.yml
Subdir.MyOtherClass.orm.yml
Can I configure doctrine to look for mapped classes in directory different than "src/SomeBundle/Entity"?
I needed to use custom prefix in doctrine mappings (doctrine.orm.entity_managers...mappings.MyMapping.prefix) to point different namespace.
I've searched a lot how to load/include a simple class in symfony 1.4 and i found nothing. I have this situation.
Under:
./apps/APP_NAME/
modules/MOD_NAME/lib
I have a class:
class MyClass {
function get_data(){
retrun "data";
}
}
Which is the best way to load in controller on symfony 1.4 the class?
Over on config exist some other way like function use_helper()?
Update: I want to load on demand, not to autoload with the convention of symfony 1.4 naming the file MyClass.class.php
Is not the best but a first version that work under module ENV is:
function load_module_lib($module, $name_file) {
require_once (sfContext::getInstance()->getModuleDirectory() . '/../' . $module . '/lib/' . $name_file . '.class.php');
}
This file allows you to define extra classes that can be autoloaded when Symfony loads. and Symfony allows you to place php code inside a .yml file
##apps/appName/config/autoload.yml
autoload:
makeupaname:
name: Make up a name
path: <?php echo $mydynamicvar; ?>/someFolder/
recursive: on
makeupasecondname:
name: Make up a second name
path: <?php echo $mydynamicvar; ?>someOtherFolder/
recursive: on
as well as go thorough this extra links autoloading namespaces in symfony 1.4 and How to create/use custom classes and helper in symfony 1.4?
Edit 01
If you want to place externel library you have to place that in lib/vendor folder. You cant create that inside app folder.
As well as if you just add new library to that and site will not load your library. So that only you have to use the autoload file to do that
This article help you out. Loading external libraries for symfony 2
I have ZF2 module, and simultaneously I use Propel genereted models hosted in root-directory/generated-classes. Can I make them share selfsame namespace - like Bookstore or so?
From Zend\Loader\StandardAutoloader I see:
public function registerNamespace($namespace, $directory)
{
$namespace = rtrim($namespace, self::NS_SEPARATOR) . self::NS_SEPARATOR;
$this->namespaces[$namespace] = $this->normalizeDirectory($directory);
return $this;
}
so if I provide two directories in Module.php, the last will prevail.
There is also:
public function setFallbackAutoloader($flag)
{
$this->fallbackAutoloaderFlag = (bool) $flag;
return $this;
}
Can I resort to it and how do I leverage this option? Any other (better) options?
I wouldn't put my models directly in /your-application/root. This will be against ZF2's recommended directory scaffolding. Instead of that, I'd create a /FooModule/src/FooModule/Model directory and put all of my models inside this folder using namespace FooModule\Model namespace definition in model class.
Another detail is; trying to pointing two different directories for same namespace is absolutely bad idea. This will be against PSR-4 Autoloading Standard and lot of open source libraries & frameworks including Zend Framework 2 which heavily depends on this standard.
I would look at the problem from a different angle. Just ask: Why I need to point one of my namespaces to the two different directories?
I think actually you mean Domain Entities by "Propel generated models". If this is correct (i mean Bookstore) is an Entity rather than a Model. You may also want to read this great answer.
So, you can try to create an Entity namespace in your Application (or whatever) ZF2 module and write your Entity classes under a sub-namespace inside that. This is perfectly valid. For example:
Application\src\Entity\Bookstore.php - namespace is Application\Entity
Application\src\Entity\Book.php - namespace is Application\Entity
Application\src\Entity\Author.php - namespace is Application\Entity
Or this is also valid scenario too (Bookstore is a module):
Bookstore\src\Entity\Book.php - namespace is Bookstore\Entity
Bookstore\src\Entity\Author.php - namespace is Bookstore\Entity
In both example scenarios, Book.php and Author.php are your auto-generated domain entities and they shares same namespace while not conflicting ZF2 or PSR-4 autoloading mechanisms.
I have a custom class named MathLib.php and I need to use some login inside this class in all the controllers. Reading CakePHP documentations I found that components are the best way to do this. But Now, I have a problem, I would like to know where do I have to save the MathLib.php class (in what Folder do i have to put custom class), and How can I load it in a component.
Thank you!
If you wrote the custom class, you put it in app\libs for cake 1.x and in app\Lib for cake 2.x, if not it goes inside the app\vendors or app\Vendor.
To load it in a component for cake 2.x you would add before your component class declaration:
App::uses('MathLib', 'Lib');
The class name and file name should be the same.
For 1.x you would load it by:
App::import('Lib', 'MathLib');
More info for 1.x here http://book.cakephp.org/1.3/view/1579/Library-classes
If it's a vendor, same idea, but read these docs: http://book.cakephp.org/1.3/view/944/Vendor-examples.
It's the file naming that's important.