i have just created a directory in blocks e.g htdocs\concrete\blocks\artists_list
Permalink
and in artists_list directory i have created 2 files
1- controller.php
contains
===================
defined('C5_EXECUTE') or die("Access Denied.");
class ArtistsListBlockArtist extends BlockController {
protected $btTable = 'btContentLocal';
protected $btInterfaceWidth = "600";
protected $btInterfaceHeight = "465";
protected $btCacheBlockRecord = true;
protected $btCacheBlockOutput = true;
protected $btCacheBlockOutputOnPost = true;
protected $btCacheBlockOutputForRegisteredUsers = true;
protected $btCacheBlockOutputLifetime = CACHE_LIFETIME;
public function artistsList() {
$query = "Showing Artist Lists";
return $query;
}
public function tester(){
$myString = 'Hello';
return $myString;
}
function save($data) {
$content = $this->translateTo($data['content']);
$args['content'] = $content;
parent::save($args);
}
}
===================================
2.i have created a view.php file
Contains
=======================
<?php defined('C5_EXECUTE') or die("Access Denied.");
$aBlocks = $controller->tester();
echo $aBlocks.'dddddddddd';
?>
================
Problem how i can call it on my default.php file
i am trying with this code
$cl = Loader::controller('ArtistsListBlockArtist/view');
$controller = new tester();
but getting error class tester() not found, what is the exact method to call controller's function in default.php file
1- controller.php
contains
===================
defined('C5_EXECUTE') or die("Access Denied.");
class ArtistsListBlockArtist extends BlockController {
protected $btTable = 'btContentLocal';
protected $btInterfaceWidth = "600";
protected $btInterfaceHeight = "465";
protected $btCacheBlockRecord = true;
protected $btCacheBlockOutput = true;
protected $btCacheBlockOutputOnPost = true;
protected $btCacheBlockOutputForRegisteredUsers = true;
protected $btCacheBlockOutputLifetime = CACHE_LIFETIME;
public function artistsList() {
$query = "Showing Artist Lists";
return $query;
}
public function tester(){
$myString = 'Hello';
return $myString;
}
function save($data) {
$content = $this->translateTo($data['content']);
$args['content'] = $content;
parent::save($args);
}
}
===================================
2.i have created a view.php file
Contains
=======================
<?php defined('C5_EXECUTE') or die("Access Denied.");
$aBlocks = $controller->tester();
echo $aBlocks.'dddddddddd';
?>
================
Problem how i can call it on my default.php file
i am trying with this code
$cl = Loader::controller('ArtistsListBlockArtist/view');
$controller = new tester();
but getting error class tester() not found, what is the exact method to call controller's function in default.php file
i know about the MVC structure but i am unable to work it with C5
========
if i follow your example
//in controller.php
public function view() {
$foo = 'something here';
$this->set('foo', $foo);
}
//in view.php
<div class="blah">
<p>My string is <?php echo $foo ?></p>
</div>
what i have to do in default.php file?
still getting this error:
Fatal error: Class 'tester' not found in D:\xampp\xampp\htdocs\concrete\themes\urban_artist\default.php on line 22
my requirement is that i create my custom block in which i get DB records and show them in default.php file.
========
if i follow your example
//in controller.php
public function view() {
$foo = 'something here';
$this->set('foo', $foo);
}
//in view.php
<div class="blah">
<p>My string is <?php echo $foo ?></p>
</div>
what i have to do in default.php file?
still getting this error:
Fatal error: Class 'tester' not found in D:\xampp\xampp\htdocs\concrete\themes\urban_artist\default.php on line 22
my requirement is that i create my custom block in which i get DB records and show them in default.php file.
Why are you trying to use a controller class in a theme template?
I think you may need to get a better understanding of MVC and concrete5 MVC in blocks first. You should not be using controllers as classes. It doesn't make sense.
You appear to be using a controller like you would be using a class in old school php. If you want to go that route you should look into concrete5 "helpers" and ignore the block structure. That would allow you to write more procedural/traditional php.
I think you may need to get a better understanding of MVC and concrete5 MVC in blocks first. You should not be using controllers as classes. It doesn't make sense.
You appear to be using a controller like you would be using a class in old school php. If you want to go that route you should look into concrete5 "helpers" and ignore the block structure. That would allow you to write more procedural/traditional php.
can i directly call the DB class here, and use the ADODB execute function to show my database list of my records, will that work?
It's pretty fugly, but you can grab a db instance anywhere as it's just a singleton.
Gives you the adodb database object.
$db = Loader::db();
Gives you the adodb database object.
If you can explain what it is you're trying to achieve, I think someone could give you a better answer about how to proceed with your architecture (from a Concrete5 perspective).
As @mkly has pointed out, using a block as a general library class doesn't make much sense -- blocks serve a purpose which is to let the people who manage the content on your site to add and edit content to pages. If you just want to retrieve some data from a database then using the Loader::db() singleton works just fine -- but it might be cleaner to implement a "model" file which wraps database access functions (this is just general good programming -- "separation of concerns" and all that -- not specific to concrete5).
But how does the data in that table get populated? If it's not via blocks on pages, then the most common approach is to create a dashboard page for the data entry. There is a fairly good tutorial that explains this process here:
http://www.concrete5.org/documentation/how-tos/developers/build-a-s...
(And after you've read that, you might find this boilerplate code I've put together helpful as well, since you said you're already familiar with other MVC frameworks):
https://github.com/jordanlev/c5_boilerplate_crud...
Hope that helps!
-Jordan
As @mkly has pointed out, using a block as a general library class doesn't make much sense -- blocks serve a purpose which is to let the people who manage the content on your site to add and edit content to pages. If you just want to retrieve some data from a database then using the Loader::db() singleton works just fine -- but it might be cleaner to implement a "model" file which wraps database access functions (this is just general good programming -- "separation of concerns" and all that -- not specific to concrete5).
But how does the data in that table get populated? If it's not via blocks on pages, then the most common approach is to create a dashboard page for the data entry. There is a fairly good tutorial that explains this process here:
http://www.concrete5.org/documentation/how-tos/developers/build-a-s...
(And after you've read that, you might find this boilerplate code I've put together helpful as well, since you said you're already familiar with other MVC frameworks):
https://github.com/jordanlev/c5_boilerplate_crud...
Hope that helps!
-Jordan
http://www.concrete5.org/documentation/developers/blocks/mvc-approa...
It isn't like an old fashioned php web page design.
For example in concrete5's flavor, you would typically do your logic in the controller(Most people don't make models for simple blocks) and pass the values to the view to display