Pregunta

I'm looking for some advice as to how I should go about designing the logic for my application in conjunction with Laravel. So far, I have the basic infrastructure created; however, I'm currently working on a League table and moving forward I want to ensure that it will be flexible enough to allow for future changes, and I also want to make use of Laravel's features more so than just the basics.

So far, I have a class 'League' which contains an 'id, title, rules, status, created_at, updated_at', and a 'LeagueController' that lists leagues that are currently open, and also allows users to sign up for each of them.

Every time a user signs up for a league, a 'LeagueEntry' is created which contains 'id, league_id, wins, losses, draws', i.e. the statistics shown in the league table. Simply enough so far :)

Within the table, the user has the option of challenging any one of the other players within the league. This is where my predicament comes in. Where I could simply create a new separate object called 'Challenge' and use a 'ChallengeController' to manage active challenges that exist between two players, I'm looking for a better way to implement this.

Another reason for this concern is that I'll be implementing a 'Tournament' class at a later point, which will also be using the 'Challenge' class (every time a player is set to challenge another in an elimination bracket).

One way of doing this would be to create an abstract class 'Challenge', and have a 'LeagueChallenge' and 'TournamentChallenge' that extend this abstraction; however, I've been told that this wouldn't be possible with Laravel. Please correct me if this is not the case. In this case, would it be best to use a Repository Design Pattern?

As you can see, I want to create my 'Challenge' class with as much flexibility as possible, and I'm looking for any of Laravel's features I may have overlooked, as well as any basic design principals I haven't thought of.

Any ideas on how to better approach this problem are greatly appreciated.

¿Fue útil?

Solución

One way of doing this would be to create an abstract class 'Challenge', and have a 'LeagueChallenge' and 'TournamentChallenge' that extend this abstraction; however, I've been told that this wouldn't be possible with Laravel.

According to your question given above you can use an abstract class and can extend it when creating sub-classes but if you need to use Eloquent by extending the Eloquent model then you can't extend two classes at the same time and it's a limitation in PHP not for Laravel, but you may use a different approach.

Just create a Base class by extending the Eloquent like this:

// Challenge Model
abstract class Challenge extends Eloquent {
    // Force Extending class to define this method
    abstract public function someMethod();
}

Then create sub-classes by extending the Challenge class like this:

// LeagueChallenge Model
class LeagueChallenge extends Challenge {
    public function someMethod()
    {
        //...
    }
}

// TournamentChallenge Model
class TournamentChallenge extends Challenge {
    public function someMethod()
    {
        //...
    }
}

You may use repository pattern in your application, it will help you to maintain and test your application easily but you are not bound to use this.

Otros consejos

I suggest you to look in to this https://github.com/LaravelIO/laravel.io , this is made by one of the laravel contributors. It is really interesting how he approaches and structures his files.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top