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
Related
So subject is the question. Yes, I've searched this forum and googled too. All I've got - useless Symfony docs and casts, some general advises, cli-s and nothing case specific. Maybe yahoo or duckduck could help better?
Everyone is talking about bundles, about how it is important to create them, probably because under the hood Symfony is pushing users away from custom libraries, but no one is actually explains how to start using a bundle - how to start calling its methods.
No, my library is not a composer or whatever package. No, library methods do not return Response objects. No, I am not dealing with composer or recompilations or cli (I use Composercat). No, I will not put library to github or packagist to load it via composer or whatever because it is private library.
Sorry about emotional off-topic.
About the case: I've put my library into the folder
src/lib/MyLibrary.php
I suspect that library class is autoloaded, because if I do not extend Controller with it (if I declare class MyLibrary instead of class MyLibrary extends Controller) - Symfony spits "class name in use" error.
So question: in my controller how to call library method?
$this->get('MyLibrary') doesn't work.
echo print_r($this) doesn't show MyLibrary in this registry too.
Looks like library file is loaded but not registered and/or instantiated. If it is so, then where to point Symfony to register it?
So most of this question is really about how php manages classes. Not so much about Symfony. But that's okay.
To start with it would be best to move project/src/lib to just project/lib. Symfony has some scanning stuff going on in the src directory and you really don't want to have your library mixed up in it.
So:
# lib/MyLibrary.php
namespace Roman;
class MyLibrary
{
public function hello()
{
return 'hello';
}
}
Notice that I added a namespace (Roman) just to distinguish your code from Symfony's.
Now you need to tweak composer.json in order to allow php to autoload your classes:
"autoload": {
"psr-4": {
"App\\": "src/",
"Roman\\": "lib/"
}
},
After adding the Roman line, run "composer dump-autoload" to regenerate the autoload files.
After that, it's just a question of using regular php inside of your application:
# src/Controller/DefaultController.php
namespace App\Controller;
use Roman\MyLibrary;
use Symfony\Component\HttpFoundation\Response;
class DefaultController
{
public function index()
{
$myLibrary = new MyLibrary();
$hello = $myLibrary->hello();
return new Response($hello);
}
}
And that should get your started.
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 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.
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.
I'm working on an Application, and most of the code will be returning in future project. So my idea was to create a structure such as this:
App/
- controllers
- models
- ...
Cake/
My_Custom_Folder/
- controllers
- models
- ..
Basically I want to add another folder next to the normal App folder and use App::build() to set the paths of the extra folder controllers, models, etc.
Now the goal is to only use the App layer when my project requires custom work, I have succeeded in that and CakePHP takes the controllers in the App over the ones in My_Custom_Folder if they are there.
For example: If i have a pages_controller.php in My_Custom_Folder AND one in my App folder, it will use the one in the App folder. If there is none in the App folder, then it uses the one in My_Custom_Folder.
However that is not how I want it to be working, what I want to do is extend the controllers from My_Custom_Folder so I can override methods and/or add new ones.
So far I have tried the following:
/My_Custom_Folder/pages_controller.php
Class PagesController Extends AppController {
Public $name = 'Pages';
Public Function home(){
echo 'default home';
}
}
/App/pages_controller.php
require_once(ROOT . DS . 'My_Custom_Folder' . DS . 'controllers' . DS . 'pages_controller.php');
Class AppPagesController Extends PagesController {
Public Function home(){
echo 'Override default home';
}
}
Unfortunately this doesn't work, it still loads the default home. Basically what i want is for it to load all methods from the My_Custom_Folder and allow the App folder to override methods and/or add methods the same way as you would by extending the Appcontroller. It does load both files, but it's not overriding any functions.
A structure like this would be great to maintain the same code over many projects and allow me to easily add new functionality for all projects by just updating the My_Custom_Folder and it would not break projects that have some customized code.
Any help on this would be greatly appreciated.
Take a look at plugin or behaviors or components or helpers. With plugins you can put them in one common folder for all apps (just like your My_Custom_Folder).