I would suggest checking out Laracasts.com and view full series on Laravel from scratch as he'll show you how to create a basic app.
I did watch the free episodes. But most of them cost something. Is it worth it to subscribe to it? Because it costs every month I think. That is much
Try to think of things as what they 'are' (Class/controller) or what they 'do' (functions/methods). As i read your question it is about the way OOP works.
In your example, you could create a vehicleController class and have the spaceshipController inherit from this class. Then the vehicleController class could have functions like:
public function doFight(){
// do fighting stuff
}
public function doMove(){
// do move stuff
}
Then if you inherit from the vehicleController you have default behavior, its then up to you how specific your spaceship fights or moves. Just create the function/method and you're off.
Now, in your example you talk about ground forces, see it like this, top off the chain is the army, the army has vehicles like tanks, spaceships or ground forces. they all can fight and move.
<?php
class armyController extends BaseController {
public function doFight(){
// do fighting stuff
}
public function doMove(){
// do move stuff
}
public function makePiece(){
// no more fighting ^^
}
}
So the groundforces could directly inherit from the armyController. For the tanks and spaceships i would create a layer vehicles.
The vehicles would extend/inherit from the armyController. It can even be a empty controller. And then let the tankController or spaceshipController extend/inherit from the vehicleController.
I hope you get what in trying to point out.
Thanks :) I think I get the principle
But can a class inherit from 2 other classes? Because everytime I need to extend BaseController so I can't extend another class?
You can keep on extending from classes as much as you like.
armyController can extend from baseController
vehicleController can extend from armyController
spaceshipController can extend vehicleController
so any function/method in basecontroller is available to the spaceshipController.
This way you can inherit/extend from any class and add the funtionality that you need. Or even overide things.
I have one more question.
I want to make a index page. On this index page you see some kind of overview. Your resources, your units etc.
When I create the route to "/index" I need to specify a Controller. What Controller do I need for displaying an overview page?
I would say a PagesController with a method "index". Is that a good way?
Thank you
No problem.
For your index a pageController sounds ok, and you want to showIndex.
As most applications/websites grow it is best practice to use meaningful names. This lets you read your code alot easier. btw it doesn't have to be "index", you could call it showOverview or what ever is meaningful for your app.
Thats the beauty of laravel, it does make us more elegant :)
I tend to create controllers based on models. So I have "UserController" for things that relate to working with a User model, a "VehicleController" for working with vehicles, etc.
In some cases I have a special controller that fits it's very specific job. For example I have a "StatusController" which is responsible for looking up and presenting vehicle maintenance status. It talks to a number of different services and repositories to assemble and deliver the status view.
As to your question about one class extending another, you may want to look into traits. Traits are a relatively new feature in PHP which allow you to plug shared functionality into classes without the limitation of class inheritance because a class can have many traits -- bits of code (properties & methods) that operate as though they were written right into the class.
Of course that does not mean inheritance shouldn't be used. I should be used where appropriate. For example the spaceship class inherits from the vehicle class if your project includes multiple vehicle types which share properties and methods.
Hopefully that's helpful.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community