Basic Application Development: Part One


Concrete5 has the tools that you will need to make a website more into an application, such as a real estate service or community portal. Concrete5 has always approached applications from a "page" level perspective. 

You create page types as a means of achieving a visual layout, or as a means of organization or achieving business logic. But sometimes you need a single page, and the reason concrete5 uses single pages instead of just php scripts is so that you can include them in your site hierarchy if you like. Just because something is custom doesn't mean that it shouldn't benefit from being a page in the site, hence single pages. 

Single pages are just php scripts that live in their own directory that are installed at that path. The dashboard is a good example of single pages. 


Adding a single page

Save your single pages into the single pages directory in the root of your site. Now we'll install it through the CMS. Go to Pages and Themes and to the Single Pages section. This section will show you all the single pages installed on your site.



You type in the path of the single page you want to add (you don't need to reference the single pages directory, you would just treat that as the root) ending with the filename without the file extension. The CMS guesses what you want to name the single page based on the filename. It will now show up in your navs and sitemap. 

Single pages can have blocks attached to them, areas, and other page properties of every concrete5 page. But they can also do a lot of other things that other concrete5 pages can't do. Single pages are wrapped in the view.php, which is a file that allows content to be injected within it, with its main content area being replaced with the injected content. View.php takes the content of the single page and renders it within the theme.


Adding another subpage beneath a single page 

To add another single page underneath an existing one, create a folder of the same name as the page you want the new single page to appear under, and then add the new single page in that folder. Add the single page at that path through the dashboard single page section. It will now appear as beneath the other single page. Alternatively, you can also create a directory and place a php file in it called view.php. This is just a matter of preference. 

To use a single page for login, you would override the login page in your local install or you can create your own login page that submits to its own custom controller if you like.


Creating a single page controller and view

Usually the point of a single page is that you require some interactivity. You manage that interactivity through a controller file. You can manage your logic and backend interactivity through the view layer theoretically but that is not in keeping with the MVC (Model View Controller) model that concrete5 is built on, and it is also more difficult to maintain. However, controllers are not required. 

To make a controller for our current single page, we can make a file in our root controllers directory called newsroom.php, or we can make a folder called newsroom with a file inside it named controller.php. 

Inside the controller file, we will start out by creating a class that extends the Controller class. It looks like so: 

class NewsroomController extends Controller {

Notice that we are basically taking out all the underscores and capitalizing each word of the filename. (In this case there is only one word, but if it had been newsroom_page, the class would be NewsroomPageController). Now, we would like something to happen when our single page is viewed. We do this by creating a view() function in our controller. Here is an example of what the view function looks like: 

public function view() {
    $color = red; 
    $this->set('selectedColor', $color);

In the view, we would also add:

echo $selectedColor; 

A quick explanation of what is happening here: In the view function, we are defining a variable. Then, we use the set controller function to set that variable's value when the page is loaded. So now, in our single page, when we echo $selectedColor, it will display the value of $color, which in this case would be 'red'. The value can be an object as well, or information about the currently logged in user, for instance. 

In our controller, we would change the contents of the view function to look like so: 

public function view() {
   $u = new User();
   $this->set('username', $u->getUserName());

We are creating an instance of the user object, which contains lots of different bits of information regarding the currently logged in user. Then, we are calling that object's member function, getUserName, which will return a string of the username.

Now in the view, you can just change the name of the variable you are echoing to $username, and it will show the name of the currently logged in user who is viewing the page. 


Single Page Tasks

In the single page, you can also run different tasks that do different things. For instance, you could have a process that adds information to a database. You would create a link in your view that looks like so: 

<a href="<?=$this->url('/newsroom', 'add')?>">Add</a>

This is using the URL method, which takes two parameters: One is the path to the page you want to go to, and the second parameter is the controller method for that page you would like to run. So in this case, this a tag would point to the path  DOMAIN/newsroom/add.  

Now we will define an add function. Keep in mind that if you didn't have an add function defined, it would throw a page not found here. Also, if you do have an add function that doesn't call the view() function, then the methods you have in your view would not run. View functions only run automatically when the page is accessed at the original url without a method appended to it. 

public function add() { 

Here you can see that the view function is called in the add function, so the methods in the view function will also be called. 

Recent Discussions on this Topic

Date picker not working in my web application

Hi I am working in the web application development using concrete5.6.1.2 the application is work when the concrete logged in the admin. for ex: i am using core concrete files in my application are [code][/code] [code][/code] but after sign out…

seperate login...

I am new to concrete5. How to set separate login for my application. concrete5 already gives the login page. But i want to use it as site editing admin. I need a another login to my application. If the application is open the home page should open af…

How to set my custom login page as startup page?

I'm creating an application using concrete5. I'm beginner of concrete5. So, how to set the login page to my application. I want to use concrete5's core login page as site editing login. If an end user open my application, at first it should show m…

Application Flow

hi, i just read the page about the application flow ( I am actually developping (or trying to do it !) a web app integrated with C5. My applicatio…