Revision: Tue, 29 Oct 2024 10:57:17 GMT
v1.0 – outdated
This version of the documentation is outdated. Consider upgrading your project to Spiral Framework 3.14
Edit this page

SchemaLocator shortcuts

Some components can be accessed from container skipping usage of get method. Use SharedTrait trait in order to enable __get proxy.

php
public function indexAction()
{
    //"db" points to `Database` via shortcut
    dump($this->db);
}

SharedTrait already used in base Controller and Service classes.

Shortcuts defined in Spiral\Core\Bootloaders\SpiralBootloader

Common Components:

Binding/Alias Component
memory Spiral\Core\MemoryInterface
container Spiral\Core\ContainerInterface (Factory + Interop Container)
logs Spiral\Debug\LogsInterface
files Spiral\Files\FilesInterface
tokenizer Spiral\Tokenizer\TokenizerInterface
locator Spiral\Tokenizer\ClassesInterface
invocationLocator Spiral\Tokenizer\InvocationsInterface

Security:

Binding/Alias Component
permissions Spiral\Security\PermissionsInterface
rules Spiral\Security\RulesInterface
actor Spiral\Security\ActorInterface

Framework Components:

Binding/Alias Component
encrypter Spiral\Encrypter\EncrypterInterface
translator Spiral\Translator\TranslatorInterface
views Spiral\Views\ViewManager
http Spiral\Http\HttpDispatcher
console Spiral\Console\ConsoleDispatcher
storage Spiral\Storage\StorageInterface

Databases:

Binding/Alias Component
dbal Spiral\Database\DatabaseManager
odm Spiral\ODM\ODM
orm Spiral\ORM\ORM
db Spiral\Database\Entities\Database (default database)
mongo Spiral\ODM\Entities\MongoDatabase (default database)

HTTP Scope Only:

Binding/Alias Component
paginators Spiral\Pagination\PaginatorsInterface
request Psr\Http\Message\ServerRequestInterface
session Spiral\Session\SessionInterface
input Spiral\Http\Input\InputManager
cookies Spiral\Http\Cookies\CookieQueue
response Spiral\Http\Response\ResponseWrapper

Only inside Routes:

Binding/Alias Component
router Spiral\Http\Routing\RouterInterface
route Spiral\Http\Routing\RouteInterface

Shortcuts will work in a static IoC scope when no local container is available. Read more about scopes here.

Use proper IDE to maximize shortcuts output:

Short Bindings

Decoupling

Consider shortcuts as temporary replacement for a proper class dependency, any shortcut can be rewritten using proper DI:

php
class HomeController extends Controller //or ControllerInterface
{
    private $views;
    
    public function __construct(ViewsInterface $views)
    {
        $this->views = $views;
    }

    public function indexAction()
    {
    //...
}

SharedTrait

You can enable shortcuts in any of your class by using SharedTrait which defines __get method. Such trait already used by Controllers, Commands and Services.

php
use SharedTrait;

public function indexAction(ViewsInterface $views)
{
    dump($this->views === $this->container->get('views'));
    dump($this->views === $views);
    
    echo $this->views->render(...);
}

Attention, SharedTrait requires method iocContainer() to be defined. Extend Componen class in order to enable such method in your classes or define it manually.