5

I'm currently in a bit of a predicament.
Here's the deal:
I want to separate my back-end from my front-end code a bit more (currently PHP code is mixed up with all the HTML, Javascript etc.. basically: front-end and back-end are one).
The question here is: how should I go about this?

In my current project, I have written some javascript code with jQuery that checks whether the user is logged in or not (checks for an auth token and UID to be present in the cookies).
However, this results in the page (in this case a dashboard that only logged in users should see) being visible for a moment before the user is redirected to the login page...

How could I go better about this (No, I won't use AngularJS for this)?

Comments
  • 1
    MVC with js in seperate .js files ? I haven't tried mvc my self but it might be of help
  • 0
    I would put the login logic server side.

    What would hapen in your case when someone disables javascript. They would stay on the dashboard they are not suppose to see?
  • 1
    Sounds like you need concrete layer definitions with boundaries (presentation/domain/data is the most common that I go by)

    Reading Clean Code will open you up to this type of thing if you want to get deeper into it.

    But here's a mini framework:

    In your presentation layer, have views, presenters, and models (and view controllers or controllers if your framework has them)

    The view controller should only instantiate the view and the presenter.
    If you don't have one, just instantiate the presenter in the view.
    View models are just plain old data structures with maybe very minor helper methods, or wrappers around domain entities.

    The presenter does the view logic, and calls to the domain for the core logic of your app(an interactor)

    The Interactor will return a value, and will access the data layer (via an injected interface) and other domain layer components.
  • 1
    There's obviously a lot more to it and it takes time to learn, but it creates that separation you're looking for. Feel free to asking any questions and I'll go into further detail.
  • 1
    You might notice that the pattern I described for the presentation layer was MVP (similar to MVC), that's because those "architectures" are actually generally best used exclusively in the presentation layer (when you use them for an entire app you tend to get either massive models or massive view controllers).
  • 0
    @neovic, thanks for your explanation, even though it gave me a huge brainfart...

    This is how I currently do it: https://gitlab.com/FinlayDaG33k/...

    basically, I include the page from a file located in the pagesdir on the serverside using PHP, then send the final output back to the client...

    While this site doesn't have a login, I do prettymuch the same for every thing: have PHP handle all the dirty work clients shouldn't have to see...
  • 0
    @FinlayDaG33k MVC.

    Learn laravel or symfony.

    Both great MVC php frameworks. You won’t look back.
Add Comment