1

I want to make a simple Laravel calculator, but I'm not sure where to define my class and where to use the functions. Do I define my class as a Model? And where do I calculate the result? Do I do it in the controller?

Say I made a class somewhere

class Calculator
{//other methods here...
    public function add($a, $b)
    {
        return $a + $b;
    }
}

And in a certain controller I should have something like this

 $a= $request->input('first');
 $b= $request->input('second');

But where do I define a new Calculator and make the addition? Do I do it in the same controller as well?

$calc=new Calculator();
$result=$calc->add($a,%b);

This obviously goes in the controller, but do I have to do the addition somewhere else and pass it to the controller?

 return view('pages.result')->with('result',$result);
astroanu
  • 3,901
  • 2
  • 36
  • 50
George Irimiciuc
  • 4,573
  • 8
  • 44
  • 88
  • View is used to display whatever information you want to display to the user and only that. The model is used for database stuff. The controller is used to "tie" the whole thing together. So your logic goes into the controller. Whatever is output by the controller goes to the view, and the model is used to grab whatever you need from the database and return it to the controller. – Andrei P. Feb 16 '15 at 08:05
  • So all the above code will go into the controller? I'm thinking that in the case of more complex apps, the controller gets quite big this way. – George Irimiciuc Feb 16 '15 at 08:06
  • The Model-Layer **is not used to represent the database**. That's nonsense. http://blog.ircmaxell.com/2014/11/a-beginners-guide-to-mvc-for-web.html ; Indeed there are multiple ways to structure your application. It's all up to you. I'm just throwing in a few bones: http://dfg.gd/blog/decoupling-your-code-in-laravel-using-repositiories-and-services , http://fideloper.com/hexagonal-architecture – thpl Feb 16 '15 at 08:10
  • I want to do things the laravel way, with the best habits. I don't want to learn any more bad ones. – George Irimiciuc Feb 16 '15 at 08:10
  • This is a pretty big topic on its own, I'd suggest you google for MVC model. To somewhat answer your question: the Calculator class that you defined should go in your controller. You would grab whatever you need from the view(from a form) pass the vars to the controller, do the math, and return what you want the user to see to the view. That's the jist of it. In your case it's not about the code itself as much as it's about thinking how the application should look. I honestly suggest a pen and paper approach at first. – Andrei P. Feb 16 '15 at 08:12
  • @thpl Yes, you're right it's not used to necessarily represent the database. On the other hand you'd have to dive deep into design patterns to explain things properly. OP is clearly still in the learning phase, instead of giving a complicated answer I chose to give a satisfactory answer. More knowledge will come in time for OP. – Andrei P. Feb 16 '15 at 08:15
  • @AndreiP. agreed but I think it's a better approach to learn it the hard way, by digging into the complicated stuff instead of taking the Quick & Dirty approach. I think it's much thougher to unlearn bad habits. – thpl Feb 16 '15 at 08:18
  • Which is why I'd like a more compicated answer as well. I already implemented it the way you said and the controller looks stuffed. I can't imagine how stuffed it would be in the case of a bigger application. – George Irimiciuc Feb 16 '15 at 08:20
  • @thpl Oh, no doubt about it. I'm sure if OP's interested to learn more google will provide the information, he just needs a starting point. Also maxell has a great blog and explains things pretty damn good, on the other hand it does require you to have some knowledge before reading his post. Either way, very good source of info. – Andrei P. Feb 16 '15 at 08:21
  • @GeorgeIrimiciuc Read http://stackoverflow.com/questions/16356420/mvc-for-advanced-php-developers/16356866#16356866 (all of this). It should give you a good grasp on what you should dig into. You should not look for the Laravel way of doing things, but how to design software properly in general. That way you will eventually become a good engineer and not just someone who mastered a framework. – thpl Feb 16 '15 at 08:28
  • Wow, that's a lot. Thanks will start digging. – George Irimiciuc Feb 16 '15 at 08:33

0 Answers0