Learn how to create controllers and display data from a database in the browser.
- [Tutor] Now to display the data on websites front-end, we will have to create a new controller. For this, we will be using our chapter two module where we will be creating a controller and our model will be used from chapter three. So by this you will be able to work with different modules and you will be able to use model from different module into controller of chapter two module. Now I click here and create a new PHP file which will be index.php.
In this PHP file, first of all I'll have to define my namespace. My namespace will be, Hplussport slash Chapter 2 slash controller slash age. Now we define a class, class name is Index. This class should extend Magenta framework app action, action.
Now inside this we will define two protected properties. First will be our $pageFactory, the second one will be a $sportFactory. Magenta uses factory design pattern to create singleton instances. Now after that we will have to define public function underscore underscore construct, which will be PHP default constructor.
Inside this constructor will use the context class which will be automatically loaded by your IDE and we will have to use our pageFactory which is in magenta framework View, result, PageFactory We will assign this to $pageFactory apart from that will have to use our sportsFactory so we will use HplusSports then chapter three because our model lies in chapter three then our model, our model's name is Sports and add the factory word after that.
Just by adding this factory keyword after your wanted class, Magenta will create a singleton instance for this model and will be creating a generated file in your generated folder. Let's assign that to $sportsfactory. Now will assign these values to our properties. First pageFactory will be our $pageFactory and then sportFactory, $sportFactory.
After that will have to define our execute function which is required for all the controllers. Now inside our execute function first of all we will create the singleton instance from our sportFactory. Let's define a variable, $votes which will be equals $this, sportsFactory and then create. Create is the default function to create the instance of that particular class.
Now will create a collection. On this collection will be using our $sports. On $sports we will be using getCollection function and on getCollection we can define like setPageSize two or you can define your sorting and other filters on your collection and after $collection we will run a for each loop.
In this for each loop will use $collection and we'll iterate over this collection. And then we just print out. For better display just write the pre just copy paste and write backslash pre and between them just print out $item get data.
getData function will bring all the data inside your variable. Now just use the exit. And let's go to our browser and refresh. Now for us, we had created a new controller so we have to change the name from view to index. Let's run this ones. Now this controller is not loaded, let's go to our terminal and run setupupgrade command once.
Now just refresh that again. Here we see the output. If you go to your database, you'll see there were three records. But in our code, we have set the page size to be two so we are seeing only two records. You can modify our collection from here. You can set up filters and sorting and all sorts of stuff from here and then you can display however you want in table or in a grid on your websites front-end based on your requirement.
- Setting up your development environment
- Installing and upgrading the schema
- Creating modules
- Magento ORM
- Creating models and collections
- Customizing Magento
- Defining plugins