new block error

Permalink
Trying to create new block to populate Tour Name area of a page but I get the following error.

Class 'Application\Block\TourNameTt\TourCMS' not found. Any ideas?

Here's the code from controller.php

<?php
namespace Application\Block\TourNameTt;
use Page;
use \Concrete\Core\Block\BlockController;
defined('C5_EXECUTE') or die("Access Denied.");
class Controller extends BlockController
{
    protected $btInterfaceWidth = 400;
    protected $btCacheBlockOutput = true;
    protected $btCacheBlockOutputOnPost = true;
    protected $btCacheBlockOutputForRegisteredUsers = false;
    protected $btInterfaceHeight = 200;
    protected $btTable = 'btTourNameTt';
    protected $btWrapperClass = 'ccm-ui';
    public function getBlockTypeDescription()

 
JohntheFish replied on at Permalink Reply
JohntheFish
You don't give enough info about the path to be sure. A guess is TourCms, not TourCMS. If not, chances are it is some other capitalisation discrepancy with the naming rules.
PJSAndo replied on at Permalink Reply
Hello Johnthefish,
I'm very new to this stuff so bear with me here.

What do you mean 'you don't give enough info about the path'?

The code worked OK when I put into a php block and dropped the block onto the page.

Now I get error 'Cannot redeclare class TourCMS'
PJSAndo replied on at Permalink Reply
OK so when I put the following code into View.php it works perfectly. I'd rather not do this though as I understand it's not good practice.
Any ideas how I might correctly transfer this code to the controller.php file?

Thanks
P

<?php  defined('C5_EXECUTE') or die("Access Denied.");
include_once('config.php');
         include_once('tourcms.php');
         $tc = new TourCMS($marketplace_account_id, $api_private_key, 'simplexml');
         $result = $tc->show_tour(xx, xxxx);
         $tour = $result->tour;
         $tourtitle = $tour->tour_name.'';
?>
<h1 class="page-title"><?php echo $tourtitle?></h1>
JohntheFish replied on at Permalink Reply
JohntheFish
Is TourCMS your class, or is it something you have obtained from a 3rd party library?

Is your block stand-alone? Or do you intend to put it into a package? If so, would it ever be submitted to the marketplace?

What is the directory structure and file structure, with full directory paths to each file in your block?
PJSAndo replied on at Permalink Reply
TourCMS is a thirdparty class defined in tourcms.php (3rd party library)

The block will be stand alone; although I may put it together with other blocks within the same page-type. (To be honest I'm not exactly sure what the significance of packages are apart from the fact that add ons and themes can be stored as packages).

No it won't be submitted to the marketplace. It's just for my website.

Concrete5
-----------Application
----------------------Blocks
----------------------tour_name_tt
----------------------------controller.php
----------------------------view.php
----------------------------db.xml
----------------------------icon.png
----------------------templates (I haven't created any templates as yet but probably will do)

Hope that makes sense.

Thanks
JohntheFish replied on at Permalink Reply
JohntheFish
I think you need to treat TourCMS as a 3rd party library in /src/. The relevant docs are in the pages beneath, particularly the last page in the section on autoloading. http://www.concrete5.org/documentation/developers/5.7/environment/r... . Unfortunately the docs are not that clear and without trying it for myself I am by no means 100% sure the following is correct, but the solution will be some minor variation of this.

The class can be in:
/application/src/TourCMS.php

and load it with
use \Application\Src\TourCMS;

or possibly just
use \TourCMS;
PJSAndo replied on at Permalink Reply
Thanks.
I currently have config.php (which 'includes' tourcms.php) and tourcms.php in root (mywebsite.com/concrete5). Do I need to move them to mywebsite.com/concrete5/application/src ?

Cheers
JohntheFish replied on at Permalink Reply
JohntheFish
Yes, the c5 autoloader won't find files in the root.
PJSAndo replied on at Permalink Reply
Thanks.

OK. Done that.
What would be the next step?
JohntheFish replied on at Permalink Reply
JohntheFish
A corresponding 'use' statement in the file(s) where you need to use the TourCMS class and methods.

The autoloader will look at the use \xxxxx; etc, locate the files and load them (its the complicated php OO way of doing what you originally did with 'include' statements)
PJSAndo replied on at Permalink Reply
So I'm guessing this goes at the top of the controller.php file.

As you said above, something like

use \Application\Src\TourCMS;

I have a couple of questions here:
1. Within tourcms.php, the tourcms class is defined as 'TourCMS'. Does it matter that the class TourCMS is defined in tourcms.php with capitals (i.e. T CMS)?

2. How do I tell the file to use config.php. Do I leave the 'include('config.php'); in the function view(), or do I use a 'use' statement as I do for the class?

Thanks
JohntheFish replied on at Permalink Reply
JohntheFish
1. Capitalisation must match exactly.

2. It all depends on what other files need access to config.php. Is this something specific to TourCMS? Does TourCMS read it, or do you reference it in your block controller and pass data from it to TourCMS?
PJSAndo replied on at Permalink Reply
So,
I put this at the top (below <?php) of controller.php

use \Application\Blocks\Src\TourCMS;

But I'm getting

Class 'Application\Blocks\Src\TourCMS' not found

This is the first line of tourcms.php "class TourCMS {" and tourcms.php is stored in application/blocks/src

I may have to stick with putting the code in the view.php for now. Shame, but at least it works.
JohntheFish replied on at Permalink Reply
JohntheFish
The file name, class name and use statement need to have identical capitalisation.

You may not need all the full path before \src\ in the use. As long as the rest is right, I think the autoloader will look in the /src/ directory anyway.
PJSAndo replied on at Permalink Reply
OK.
Now I get

"Cannot redeclare class tourcms"
edbeeny replied on at Permalink Reply
edbeeny
When I get that error I have overwritten the wrong file. Check you have not got a duplicate file, like 2 controller files.
paulslugocki replied on at Permalink Best Answer Reply
Hi PJSAndo,

Paul from TourCMS here.

I think the issue is you are including the tourcms.php file, then also the config.php file which itself includes tourcms.php.

https://github.com/TourCMS/tourcms-php/blob/master/config-example.ph...

You might find it easier to ditch the config.php, just including the tourcms.php and creating an instance of the TourCMS class, filling in the API credentials in your Concrete code.

$tc = new TourCMS($marketplace_account_id, $api_private_key, "simplexml");
PJSAndo replied on at Permalink Reply
Hi Paul,

Thanks for your reply.

Yes, eventually figured it out. It's now working, pulling in tour data from TourCMS successfully and integrating website enquiry/brochure form with TourCMS. Very cool.

P