0

Is it right to use php code or blade code in view file using laravel?

for MVC consideration is it better to sepearate front end code like HTML with any server side code and keep them in corresponding controller file ?

for example using like this:

in view:

    <table>
      <Loop>  <!-- instate of using @foreach -->
        <tr>
          <td>
            <-UserId->
          </td>
          <td>
            <-UserName->
          </td>
        </tr>
      </Loop>
    </table>

in controller:

    $html = view('page');
    $loop_section=my_own_func_to_get_loop_tag_content($html);

    //edit loop_section var with php foreach and return resault to  $modified_loop_section var

    $html=str_replace($loop_section,$modified_loop_section,$html);

    return $html;

Updated:

above code is just an example way that isn't seemed a good way. but i look for better way to separate any php code (including if foreach etc) with html code in view file without using a custom tag and code?

Adel Lotfi
  • 27
  • 4
  • This is a misunderstanding of MVC, and needlessly complicates everything. Use Blade's `@foreach` etc. as documented - that's entirely normal in MVC. What you *shouldn't* be doing is stuff like your database queries in the view - they should be done in the controller and *passed* to the view. – ceejayoz Sep 15 '18 at 22:14
  • I use that for avoiding problems in team works, so i did separate any php (or blade) codes (like foreach) from view file that writing by html programmers. my question is this way logical? or have anyone a better way? – Adel Lotfi Sep 15 '18 at 22:53

1 Answers1

-2

Yes you should definitely use Blade code in your templates. So function my_own_func_to_get_loop_tag_content should be processed in template file, but it should only show data. All business logic (such as DB querying, sorting, calculations, ...) should be done in your controllers. View is only for displaying data, so there shouldn't be any logic.

Jozef Cipa
  • 2,133
  • 3
  • 15
  • 29
  • 1
    `@foreach`, `@if`, etc. are logic, but they (potentially) belong in the view. – ceejayoz Sep 15 '18 at 22:14
  • So you mean if we have a person to work on html and another person to work on php , they must work together exactly in a view file ?? Is it logic? why dont we separate view with any php code completely? – Adel Lotfi Sep 15 '18 at 22:19
  • @ceejayoz yes, absolutely agree. I didn't mean this. Of course you need `@foreach` and `@if` in your views but only for conditional displaying some html parts, not for tasks like e.g. loading data from db based on if condition – Jozef Cipa Sep 15 '18 at 22:27
  • imagen that a person that works on html dont know any php code.I wonder for mvc consideration isnt better to use all php codes (including if foreach etc) outside of html file ? – Adel Lotfi Sep 15 '18 at 22:38
  • @AdelLotfi No. Your custom tags that don't exist in HTML would be equally confusing, with the added bonus of confusing the PHP users too. **Everyone** would be confused by your proposed approach - it introduces a bunch of complexity and confusion with little benefit. – ceejayoz Sep 15 '18 at 23:41
  • @ceejayoz yes i agree. i'm making a confusing way to html and php users with custom tags. and that's why i asked a question here to hope that a person show me a good way that have separated php and html completely in view file and without any custom tag and code... – Adel Lotfi Sep 16 '18 at 08:27
  • @AdelLotfi I don't think that's a reasonable goal. Anyone messing around in the code of a Laravel app should have at least a basic understanding of PHP/Laravel. You can teach a designer Blade's templating system. – ceejayoz Sep 16 '18 at 11:59
  • @JozefCipa you might find this to be a relevant read: https://stackoverflow.com/a/16596704/727208 – tereško Sep 19 '18 at 08:21