Vibe.d tutorial
  • Build web apps with Vibe.d
  • Setting up
  • The default hello world app
  • Use your own HTML page
  • Serving other static files
  • Diet templates
  • More template pages
  • Use your own functions
  • Templates for ease of maintenance
  • Using include in templates
  • Responsive layout using CSS Grid
  • A fixed navbar and sticky footer
  • CSS modal dialogues
  • The web interface
  • Setting up MySQL server and tools
  • The schema
  • Setting up MySQL for Vibe.d
  • The EmployeeController class
  • The EmployeeModel class
  • A form for adding a new employee
  • Saving form data into the database
  • Testing the whole thing
  • Listing all the employees
  • Retrieving a record for editing
  • Saving form changes to the database
  • Deleting a record from the database
  • Finding an employee record by name
  • Displaying error messages with _error
  • Authentication and authorization
  • Logging in and authentication
  • Saving the login state to the session
  • Enforcing authorization through the session
  • Logging out
  • All the sources so far
  • A new project
    • The timekeeping system
Powered by GitBook
On this page

The EmployeeController class

The EmployeeController class will be the embodiment of the web framework. It will also be the controller for linking our views with the data (the models).

Edit source\empcontrol.d:

module empcontrol;
import vibe.vibe;
import empmodel;

class EmployeeController
{
  private EmployeeModel empModel;
  private string realm = "The Lorem Ipsum Company";
  
  this()
  {
    empModel = new EmployeeModel;
  }
  
  void index()
  {
    render!"index.dt";
  }
  
  void getAddEmployee()
  {
    render!("empadd.dt", departments, provinces, paygrades);
  }
}

In D, the constructor is this() while the destructor is ~this(). In the constructor, we instantiated an empModel variable so we can start using the EmployeeModel class.

The method

render!"index.dt";

is a variadic function template. It means render!() can have a varying number of parameters (arguments) of different data types. We have seen this construct before with staticTemplate!(). In D, if there is only one parameter, the parentheses are optional.

In this case,

render!("empadd.dt", departments, provinces, paygrades);

render!() has three arguments (or parameters), one string and three arrays, so we have to enclose them in parentheses.

Let us test by running the app.

Sometimes, instead of seeing a successful compilation, you might see several lines of errors instead. The usual culprit is the indentation. In that case, click on the ‘Tab Size:’ prompt at the bottom of VS Code for each file that cause an error:

And choose to either convert all tabs to spaces or all spaces to tabs from the choices at the top of VS Code to make indentations uniform and consistent and make sure all indentation rules are followed, then save the file again.

We indicated that we are going to use an EmployeeModel class which we haven’t created yet, so let’s do that next.

PreviousSetting up MySQL for Vibe.dNextThe EmployeeModel class

Last updated 5 months ago