Create Controller
You can simply create a new controller for an existing app by:
(venv) python3 -m manage create-controller
It will prompt you for some required arguments.
As an example, here we will create a controller called Index
for an existing app called notes
:
(venv) python3 -m manage create-controller
App Name: notes
Controller Name: Index
Controller URL (optional):
Methods (optional): get, post
It will create the new controller for you, and also updates the _controllers.py
module of the selected app.
-
The Controller Name must be in "CamelCase" form with at least two "a-z" and "A-Z" characters.
-
Valid characters for Controller URL are:
a-z
, 0-9
, -
, /
, <
, :
, >
-
Valid Methods are: post, get, put, delete
You will learn more about these methods at RESTFUL CRUD Methods section.
Notes!
Even though the Controller URL is optional, if you leave it empty it will become required for the next controller.
If you leave the methods empty, the framework by default considers the GET method for the selected controller.
Here are some examples for valid Controller URLs:
Do
Controller URL: index
Controller URL: my-notes
Don't
Controller URL: notes/index
The framework considers this URL as /notes/notes/index/
, which doesn't exists!
Do not add the App URL to the Controller URL. It will be handled by the framework.
Generic URLs
You can also create generic URLs using the following data types:
Controller URL: greet/<name>
Controller URL: greet/<str:name>
Controller URL: edit/<int:id>
Note!
Do not use /
at the beginning and ending of your Controller URLs. It will be handled automatically by the framework itself.
Using Controllers
Here's the controller class we created in the previous chapter of this section:
It is the blueprint of your controller created by the Aurora framework.
On the first line it imports the Controller
, and View
classes of Aurora framework.
The current controller inherits the Controller
class, with the given methods.
If you need __init__
magic method, please do not inherit the parent class:
Don't
Using the code above allows the users access to all methods of the parent class in the current controller.
For this specific example even to PUT
and DELETE
. This is something that you don't want.
You can still use all magic methods for this class. Only remember, not inherit the parent class in the constructor method of the current class:
Do
However, it is possible to import the Flask request method and handle the methods manually, but this is useless and doesn't make any sense:
Don't
Do
If a user tries to access a Controller class with a method that is not defined, the errors
app will handle it with a 405 forbidden method
, and you don't need to do anything.
Here we returned a simple text in the GET
method of the current controller.
You can use the View
class to return rendered HTML to the browser:
It will try to render a view template called index.html in the /views/notes/
directory (which doesn't exist yet).
Note that you don't need to provide the .html
for the view, only the view name is enough.
Modifying Controllers
If you already created a controller for an existing app and want to modify it, you should do it manually in the _controllers.py
module of the desired app.
For the notes app here's how it looks like:
For example, you can remove the post method:
Important Note!
Please do not touch lines with the flag #do-not-change-me on this framework, otherwise it stops working as expected.
Then, you also need to remove the post method from the Index controller of the notes app:
You can do the same for GET
, PUT
, and DELETE
methods.
You can also add a method. Only remember to update both _controllers.py
and Controller modules for the current app.
If you want to use a controller more than once, you cannot use the create-controller
, because it considers the requested controller as an existing one and gives you an error message.
However, it's possible to add it manually in the _controllers.py
module:
Important Note!
However, the URLs must be unique, otherwise your application stops working and raise an AssertionError
.
Delete Controller
You can simply delete an existing controller for an existing app by:
(venv) python3 -m manage delete-controller
It prompts you for App Name and Controller Name. After confirmation of the data loss, it will delete your controller permanently.