lilly
lilly

Reputation: 701

Silex, loading own classes

I play around with Silex, PHP micro-framework. At the moment I try to load my own classes but I have no luck doing it. Maybe somebody could explain me a little how does loading in Silex works?

My project structure looks like this:

app/
vendor/
web/
tests/
bootstrap.php
composer.json
composer.lock

Let's say I want to load a class Controller\User (namespace here) from /app/MainController.php.

How can I do that? I've browsed some articles (loading via Composer or Symfony's UniversalClassLoader), followed some instructions, and still it does not work.

If someone could give me a hand with it please, I would appreciate it.

Upvotes: 4

Views: 7949

Answers (3)

Peyman Mohamadpour
Peyman Mohamadpour

Reputation: 17944

As of October, 2014 the best way to autoload your own classes into silex, is using PSR-4 (instead of PSR-0) autoloading through Composer using composer.json file in your application root:

{
    "require": {
        "silex/silex": "~2.0",
    },
    "autoload": {
        "psr-4": {"Vendor\\Namespace\\": "path/to/src/"}
    }
}

After editing your composer.json you need to do a:

composer update

on your project root directory, using terminal.

Upvotes: 2

spectrumcat
spectrumcat

Reputation: 43

I was also looking for the same thing, since Silex documentation still has the registerNamespace function that was removed.

Found a really nice answer here

In Short, all you have to do, is add a "psr-0" in "autoload" section in the composer.json

Example composer.json:

{
    "require": {
        "silex/silex": "~1.3",
        "doctrine/dbal": "~2.2"
    },

    "autoload": {
        "psr-0": {
            "MyApp": "src/"
        }
    }
}

Make sure to update the Composer Autoloader ("composer install" or "composer update")

Upvotes: 3

Fatal705
Fatal705

Reputation: 153

I assume you load your Silex classes in bootstrap.php like that:

// bootstrap.php
require_once __DIR__.'/vendor/autoload.php';

If so replace this code with the following:

// bootstrap.php
$loader = require __DIR__.'/vendor/autoload.php';
$loader->add(YOUR_NAMESPACE, DIRECTORY_OF_THE_NAMESPACE);

Example:

// bootstrap.php
$loader = require __DIR__.'/vendor/autoload.php';
$loader->add('Tutorial', __DIR__.'/src');

You can add multiple namespaces if you like, just call the add method for every namespace.
In src/ you would store your class files. For every namespace create a folder which contains your class files for this namespace. The file should have the same name as the class.

MyNamespace\MyClass => src/MyNamespace/MyClass.php
MyNamespace\SubNamespace\SubClass => src/MyNamespace/SubNamespace/SubClass.php

In every class file you have to set the associated namespace in the first line.

// src/Tutorial/DemoController.php
namespace Tutorial;
class DemoController{
// class definition goes here..
}

You have now access to your classes in every file which includes bootstrap.php.
In /app/MainController.php you can now access your own class like this:

// app/MainController.php
use Tutorial\DemoController;
$foo = new DemoController();

This solution worked for me. Hope it works for you.

Upvotes: 12

Related Questions