How can I use a PHP class in a Block Controller

Permalink
I tried to use a PHP Class in a Block Controller.

I tried to define the class like you can see in the following code.
<?php 
namespace Application\Block\SgArticle;
defined("C5_EXECUTE") or die("Access Denied.");
require_once ("application/tools/sg_tools/sg_contr.php");


Then I tried to call a static function from this class like you can see in the following code
SgContr::addSgCommenCssHeader($this);


But I allways get the following failure:

Class 'Application\Block\SgArticle\SgContr' not found

public function on_start()  {
       SgContr::addSgCommenCssHeader($this);
...


Can any body help me?

GunterSchmitt
 
Gondwana replied on at Permalink Reply
Gondwana
You might need a 'use' statement, or prefix SgContr with its namespace.

I didn't test this.
GunterSchmitt replied on at Permalink Reply
GunterSchmitt
I also tried to use the following and it also does not work:
use \Application\Tools\SgTools;

or
use \Application\Tools\SgTools\SgContr;

in the class I set

namespace Application\Tools\SgTools;
defined("C5_EXECUTE") or die("Access Denied.");
JohntheFish replied on at Permalink Reply
JohntheFish
the namespaces work most consistently beneath the /src/ directory.

So file
application/src/MyClass.php
and
namespace Application\Src\MyClass;
and
use Application\Src\MyClass
ob7dev replied on at Permalink Reply
ob7dev
Custom classes usually go in a 'src' folder.

So if I have a package:
/packages/my_package/

and I write a custom class for this package:
namespace Concrete\Package\MyPackage\Src\MyCustomClass;
class MyCustomClass extends Controller {


I would save this file to:
/packages/my_package/src/MyCustomClass/MyCustomClass.php

then I would access it else where via its namespace, so at the top of another file that uses this class I would write:
use \Concrete\Package\MyPackage\Src\MyCustomClass\MyCustomClass;

Then else where in this file I can access its functions/methods via:
$thisVar = MyCustomClass::anAvailableFunction();
GunterSchmitt replied on at Permalink Reply
GunterSchmitt
Thank you.

How can I handle this if I have a class for more than one Custem Block. E.g. one class for all blocks they have to use the same structured data.

I'll try to write the code once and use it many times.
ob7dev replied on at Permalink Reply
ob7dev
I think in that case, and considering its not being packaged but lives inside your application folder. You should be able make a directory called 'src' inside your application folder (considering theres one in the concrete folder).

Let me know if that works, although I've never done that before as I like keeping everything packaged.

But now that I think of it, you should be able to still access the class from anywhere else in the application, other blocks included. You just need to use the 'use namespace\of\class' at the top of the file and the custom class should be available.
GunterSchmitt replied on at Permalink Reply 1 Attachment
GunterSchmitt
Hi,

I tried to use a package (see attached file) but it does not work. I see the Block but if I try to add the block it will not insert into the page (I think there will be a loop).

Can you be so kind and have a look?

Thanks in advance

Gunter
Gondwana replied on at Permalink Reply
Gondwana
In blocks/text/controller.php, after the 'use' statements, try:
require_once ("packages/sg_testa/src/sg_contr/sg_contr.php");
GunterSchmitt replied on at Permalink Reply
GunterSchmitt
Thank you, this works fine.