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.
Then I tried to call a static function from this class like you can see in the following code
But I allways get the following failure:
Class 'Application\Block\SgArticle\SgContr' not found
Can any body help me?
I tried to define the class like you can see in the following code.
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?
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
So file
application/src/MyClass.php
and
namespace Application\Src\MyClass;
and
use Application\Src\MyClass
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:
I would save this file to:
then I would access it else where via its namespace, so at the top of another file that uses this class I would write:
Then else where in this file I can access its functions/methods via:
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();
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.
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.
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.
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.
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
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
In blocks/text/controller.php, after the 'use' statements, try:
require_once ("packages/sg_testa/src/sg_contr/sg_contr.php");
Thank you, this works fine.
I didn't test this.