Procedure to create singke_page

Maybe it’s because I have a mild reading disability but as I tried to find the answer to this in the documentation I just got more and more confused.

I need to add a single_page and on that page I will display results from a MySQL search. What would the procedure be to add the single page and where would I put my php code to loop through and display the results. I can figure out how to make my part of this work. I can’t figure out how to create the single page and where in it to put the code.
I’m not sure if I need a controller because one of the answers here just talked about putting the page at application/single_page/mypages/thepage.php
Elsewhere I saw that I also needed a view.php so I added that in the same directory. Do I need a controller (what would go on it) . I’m sorry to sound kinda dumb on this but I’ve always found the documentation very difficult to work through

It would be advisable to use a controller just to do your query and then pass the results into the “view” part of your single page, it is not required.

If you want to use a controller you would place it at application/single_page/page_name.php then the controller should contain something like this

<?php
namespace Application\SinglePage;

defined('C5_EXECUTE') or die("Access Denied.");

class PageName extends \Concrete\Core\Page\Controller\PageController
{
    public function view()
    {
        $results = //do your query stuff here and get your results.
        $this->set('results', $results);
    }
}

Then your single page would be placed at application/single_pages/page-name.php (yes the directory is plural and the filename has a hyphen even though the controller has an underscore) and you can have whatever output you want in it, keeping in mind it will use the view.php file from the theme as it’s wrapper (so no need for header footer) and if you used the controller you will have a variable $results already there for use.

After you have created the file(s) you will need to go into the Dashboard → Pages & Themes → Single Pages and use the Add Single Page box to install your my-page (do not include “.php”) once you’ve installed it you should be able to navigate to that page and see your output.

Wow, that seems pretty simple. I’ll work on it over the next little while. Can I have more than one array like this:
$results=
$balloon=
$this->set(‘results’ $results);
$this->set(‘balloon’ $baloon);

I’m realizing that I have a couple JS assets and I’m not sure where concrete recommends they get placed (which directory). I’m a pretty weak Php writer so is there a recommended “include” statement so I get the right directory without a long string of …/…/the-JS-directory/wherever.js kinda include statement. Sorry if that’s a question I should know. I would like to try and make this as clean as possible Thanks.

Yes, you can set as many variables as you want that way and they’ll get passed from the controller into the view.

I believe you can put stuff in the application/js directory and then use the same methodology as the “Registering an asset in a block” here Registering an Asset - Concrete CMS Documentation but instead of blocks/xx/xx just put js/asset_name.js and put it into your controller. You’ll then need to call the requireAsset from the view function.

Well I’m at a loss. I’m clearly doing something wrong. I setup the controller and the view page in application/single_pages/map_results/
map-results.php is the view page and
map_results.php is the controller.
I’ve refreshed the single_page in the menu every time i’ve made a different attempt. I have st the page to not cache (I’m wondering if there is script I can include in the page to do that programatically)
Here is the beginning of my controller:

<?php
namespace Application\SinglePage;

defined('C5_EXECUTE') or die("Access Denied.");

class Map_Results extends \Concrete\Core\Page\Controller\PageController
{

    public function on_start()
{
    $al = \Concrete\Core\Asset\AssetList::getInstance();
        
        $al->register(
        'javascript', 'dw_event', 'js/dw_event.js'
    );
        $al->register(
        'javascript', 'dw_scroll', 'js/dw_scroll.js'
    );
        $al->register(
        'javascript', 'dw_scrollbar', 'js/dw_scrollbar.js'
    );
        $al->register(
        'javascript', 'scroll_controls', 'js/scroll_controls.js'
    );
        //Register the JS assets
    $al->registerGroup('scrollbar_scripts', array(
    array('javascript', 'dw_event'),
    array('javascript', 'dw_scroll'),
    array('javascript', 'dw_scrollbar'),
    array('javascript', 'scroll_controls'),    
));
}  
    //Require the JS assets
    public function registerViewAssets($outputContent = '')
{
    $this->requireAsset('scrollbar_scripts');
}
    //Start the View
    public function view()
    {   
//the database queries are here  and ends with the passing of the query results:
       $this->set('regiondata', $regiondata);
        $this->set('startiondata',$stationdata);
        $this->set('regionname',$regionname);

The view page has some HTML javascript etc.
When I try connecting to the page it appears to exist but he content is empty. There are no errors. Was there a script I needed to place at the beginning or just treat it as a typical html/php page?

As a follow up I found this post:

Single page controller

and moved the controller to application/controllers/single_page/
and now the page view runs but the controller is not running. I’m totally confused! I feel like I’m finding different post that use different locations and different naming conventions for the controller and the view file. I’m really lost.
Just to add. As I left it I followed the documentation for a single page.
The controller is at /application/controllers/ single_page/mapresults.php
And the view file is at
/application/ single_pages/mapresults.php

Before anyone wastes time answering, I figured out where I went wrong. I’ll post it here for anyone else. I just can’t do it right now.

Here is how I made this work:
The Controller was placed at /application/controllers/single_page/mypage
and the namespace is:

<?php
namespace Application\Controller\SinglePage;
use Concrete\Core\Page\Controller\PageController;
class MapPage extends PageController
{
    //Start the View
    public function view()
    {

Note the <?php tag. The Controller seems to requires it. and also closing it at the end of the file with the ?>
The only way I could get the view file to work was to place it at:
/application/single_pages/mypage/view.php

I tried putting it in:
/application/single_pages/mypage.php
and I tried naming it MyPage.php, mypage.php,my-page.php,my_page.php etc. None of them worked. The controller would only engage it it was the way I show here.

Maybe there is another way, but this worked for me after trying a multitude of other thing. I hope this helps another newbie!
Thanks for your help @hutman your direction was helpful.

1 Like

Glad you got it working! Sorry I didn’t see your responses, the forum notification emails don’t seem to get through to my inbox anymore.

1 Like