Deprecated Code Reference (ongoing)

Deprecated Code Reference (ongoing)

This doc holds a reference list of commonly used code that is deprecated, and their replacements. These functions may still work and exist now, but can be removed from future versions of concrete5 and thus cause problems in the future.

This is a work in progress. This list will change as new functions are found and new versions are released.

Note:

In the replacements you will sometimes find references to $app. You can access the application in some controllers with $this->app, or if it’s not available, retrieve it with:

$app = \Concrete\Core\Support\Facade\Application::getFacadeApplication();

Using $app may result in faster code.

Class Aliases

Version 5.7 introduced class aliases to make some of the commonly used core code easily available for developers. This idea was mostly due to the old 5.6 architecture where this was very common. This has, however, caused more confusion than benefit since people are finding it hard to follow where the code originates from.

Therefore, class aliases have been deprecated and full namespaces should be used in the future. A full list of aliases can be found from the concrete/config/app.php file.

Incorrect:

<?php
namespace Application\SomeStuff;

use Area;
use Block;
use Page;
// etc.


class MyClass
{
    // ...
}

Correct:

<?php
namespace Application\SomeStuff;

use Concrete\Core\Area\Area;
use Concrete\Core\Block\Block;
use Concrete\Core\Page\Page;
// etc.


class MyClass
{
    // ...
}

Facades

Generally it is suggested to define your object's dependencies in the constructor rather than using the faceades.

Incorrect:

<?php
namespace Application\SomeStuff;

use Concrete\Core\Support\Facade\Application;

class MyClass
{
    public function doSomething()
    {
        $app = Application::getFacadeApplication();
        $db = $app->make('database')->connection();
        if ($db->tableExists('Areas')) {
            $this->doSomethingElse();
        }
    }

    // ...
}

Correct:

<?php
namespace Application\SomeStuff;

use Concrete\Core\Database\Connection\Connection;

class MyClass
{
    protected $db;

    public function __construct(Connection $db)
    {
        $this->db = $db;
    }

    public function doSomething()
    {
        if ($this->db->tableExists('Areas')) {
            $this->doSomethingElse();
        }
    }

    // ...
}

With this type code, you will need to initiate your class through the IoC container for it to resolve the dependencies automatically:

<?php
$my = $app->build('Application\SomeStuff\MyClass');
$my->doSomething();

Sometimes this might not be possible e.g. when modifying a class that already has existing constructor functionality. In those cases it is still OK to use facades.

Loader functions

Loader::db() - deprecated since 5.7

replacements:

$db = Database::connection();

$db = $app->make('database')->connection(); /* @var $db \Concrete\Core\Database\Connection\Connection */

Loader::helper() - deprecated since 5.7

replacements:

$pkgh = $app->make('/packages/' . $pkgHandle . '/helper/' . $service);

$h = $app->make('helper/' . $service);

examples:

$th = $app->make('helper/text');

$nh = $app->make('helper/navigation');

$ps = $app->make('helper/form/page_selector');

$al = $app->make('helper/concrete/asset_library');

$color = $app->Cmake('helper/form/color');

$form = Loader::helper(‘form’) - deprecated since 5.7

replacement:

block/view.php: $form is already defined

Loader::packageElement() - deprecated since 5.7

replacement:

$ve = View::element($file, $args, $_pkgHandle);

Loader::element() - deprecated since 5.7

replacement:

$ve = View::element($file, $args, $_pkgHandle);

Loader::model() - deprecated since 5.7

*replacement: *none

Loader::library() - deprecated since 5.7

*replacement: *none

Loader::controller() - deprecated since 5.7

replacement:

in a page: Page::getPageController();

BlockType

BlockType::installBlockTypeFromPackage() - deprecated since 5.7

replacement:

$bt = BlockType::installBlockType($handle, $pkg);

Database

Database::get() - deprecated since 5.7

replacement:

$db = Database::connection();

$db = $app->make('database')->connection(); /* @var $db \Concrete\Core\Database\Connection\Connection */

$db->execute() - deprecated since 5.7

replacement(s):

$db->executeQuery($q, array($arguments));

($q instanceOf \Doctrine\DBAL\Statement): $q->execute($arguments);

$db->GetOne() - deprecated since 5.7

replacement:

$res = $db->fetchColumn($q, array($arguments))

$db->GetAll() - deprecated since 5.7

replacement:

$res = $db->fetchAll($q, array($arguments))

File

$f->isError() - deprecated since 5.7

returns false

replacement: none, (File::getByID() != null)

Controller

PageController::isPost() - deprecated since 5.7

replacement:

$this->request->getMethod() == 'POST'

PageController::post() - deprecated since 5.7

replacement:

$this->request->request->get()

PageController::get() - deprecated since 5.7 (for HTTP GET parameters)

replacement:

$this->request->query->get()

PageController::get() can still be used to fetch the variables that have been made available through PageController::set(). In case you are fetching HTTP request parameters, you should be using the above method insteead.

Permissions

FilePermissions::getGlobal() - deprecated since 5.7

replacement:

$fp = new Permissions(FileSet::getGlobal());

TaskPermission() - deprecated since 5.7

replacement:

$tp = new Permissions();

Request

Request::getInstance()

replacement:

$r = $app->make(\Concrete\Core\Http\Request::class);

UserInfo

UserInfo::getByID()

replacement:

$ui = $app->make(\Concrete\Core\User\UserInfoFactory::class)->getByID();

Loading Conversation