Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Best Practice
  • what is the best practice

    Session::set_flash('error', 'Email already exists');

    or

    \Messages::error(__('login.email-already-exists'));

    ----------------------------------------

    $data['products'] = array(); //stores products
    return View::forge('home/index', $data);

    or
    $products = array(); //stores products
    $view->set_global('products', 'products');
  • HarroHarro
    Accepted Answer
    That depends on what "Messages" does. If that is the message class I once made (and it used in for example the old Depot code), that does a set_flash() call too, but supports multiple error messages.

    And I detest global variables. In PHP, and for Views... You're doing something wrong if you need globals... ;-)
  • I asked this question by a code of a more skilled person, and i don't know why he use globals
  • HarroHarro
    Accepted Answer
    Probably the same reason as anyone: it's easy.
  • I don't know why the CRUD generate bu OIL in the ACTION_EDIT method use GLOBAL

    example: $this->template->set_global('user', $user, false);

    If this is a bad practice?
  • Oil generation uses it because the create and edit vIews that are generated both load a secondary _form view, and they need to pass the data along.

    For this to work correctly you need PHP 5.5, and since v1 supports 5.3.3 upwards, it could not be implemented.

    With 5.5, you can use

    \View::forge('some/_form', $this->get());

    to pass all data passed to the parent view on to the child view, so you don't need to set them globally.

    There is quite a bit bad practice used here. There should not be any code in view files, other than some display logic (echo, if's or simple loops). Loading a View is logic.

    Ideally, you would use a Presenter for Create and Edit. The presenter would create the _form View instance, passes only the variables required in that view, and then passes the created view instance to the parent form. So all it has to do is

    echo $from; // or something...

    to display the generated form.
  • I prefer use simple html in the view and don't generate the complete form 

    too there is another way to pass the data to _form it is using :

    $data['data'][['users'] =  users data to edit view

    \View::forge('some/edit', $data['data'][['users'] );

    and in the edit view \View::forge('some/_form', $data['users']);

    is this a bad practice?




  • HarroHarro
    Accepted Answer
    $__data.

    Yes, that works indeed, but is not officially supported. Still, it's using code in a View.

    As a quick test: say you use a template engine (for example twig) that does not support PHP, would your solution still work. If the answer is no, you're likely using code in your view that doesn't belong there.
  • thanks now I will separte all php code from the view.

    this is apply for the conditions (if/else) I guess


Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion