Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
my route code not work
  • hi 
    where is wrong in my route code ? 
    i want to post  my form to articles/comments/addcomments/12   then route to articles/comment/add/12 in hmvc approach


    <?php 

    return array(
        'articles/comments/addcomments/(:any)' => function () {
       
                return \Request::forge('articles/comment/add/$1')->execute();
        
    },
    ); 

    12 not pass to request path

    i try this too but  my problem didn`t  solve 

      
    <?php 

    return array(
        'articles/comments/addcomments/(:any)' => function ($name) {
        
                return \Request::forge('articles/comment/add/$name')->execute();
        
    },
    ); 





  • Where in the docs does it state that inline routes support any parameters?

    And why use an inline route for this, you're not adding any logic, so a normal route mapping would work fine too?
  • i want to have some classes for separating widgets and normal logic classes, for this i created a base class that other HVMC classes inherit from it, in constructor (before) function of base class i wrote these lines 

    if ( ! request::is_hmvc())
    {
    return httpnotfoundexeption
    }

    btw: i have a widget with a form, the form is created by hmvc pretty well 
    but when the form is submitted by the user since that post request isn't a hmvc request 404 exception occur in base class.

    i try to solve this issue by submit my form to another class that is out of hmvc classes , the result was occurred another problem ,that was  instance of the fieldset that i created in hmvc function returned NULL .

    btw i stop trying to find better solution for this issue i handle all widgets in normal class now my issue solved 

  • You should not return an exception, you should throw an exception.

    My point is that

    return array(
        'articles/comments/addcomments/(:any)' => function () {
                return \Request::forge('articles/comment/add/$1')->execute();
    },

    is functionally the same as

    'articles/comments/addcomments/(:any)' => 'articles/comment/add/$1',

    which the diffference that this one does work, and it's one request layer less so it's faster. It also makes is_hmvc() work, in your code ALL requests are HMVC requests due to the additional request in the inline route.
  • thank you for the time you allotted to me  

    yes you are right ,throw is correct. i use throw in my code too.

    i have created a github from the basic example of my issue  to show you in my code that   is_hmvc() returns false 

    in my code i have one class named frontend for controller and  one theme named frontend and one module named "main".

  • HarroHarro
    Accepted Answer
    What is exactly the problem?

    The form loads fine here, and if I request the URI directly, I get a 404.

    If I replace your Controller_hmvc before() method with a var_dump(\Request::is_hmvc()), it returns true when I request the root and your form is displayed, and false if I request "/main/hmvc/myform".

    So I'd say "works as advertised"?
  • ok i think i've had a bad logic that i put the code in before function of controller_hmvc class because i have a widget with a form then so post request has to request the uri directly 
    And because 404 occurs i cant handle posted form  
  • HarroHarro
    Accepted Answer
    Normally you post a form to the same URL, and after processing the form, redirect away, which will prevent users reloading the form in the browser.

    If you want to split the logic for displaying the form and processing it in the controller, you can use "get_methodname" and "post_methodname" instead of "action_methodname".
  • i want to question you  one another problem in fieldset class because you have my source example.

    i  splitted logic form from displaying as  you talked.
      
    i forge fieldset with unique name in action_myform() and i want to instance it in post_myform() for validating inputs 

    but why does  var dump return me false in post_myform()
     
    public function post_myform()
    {
    $testvar=\Fieldset::instance('form');
    var_dump($testvar);
    echo \input::post('title');

    }
    public function action_myform()
    {
    $fieldset =  \Fieldset::forge('form'); 
    $fieldset->add('title', 'title', array('maxlength' => 50), array(array('required')));
    return \Response::forge($fieldset);
    }
  • HarroHarro
    Accepted Answer
    PHP applications are stateless, you can't carry anything from one request to the next, unless you store it into the session.

    So if you split the action into a separate get and post, you end up duplicating some of your "prepping" code, like forging the fieldset.

    You could work around that by moving that to it's own protected method:

    public function post_myform()
    {
         $testvar=$this->formfieldset();
         var_dump($testvar);
         echo \input::post('title');
    }

    public function get_myform()
    {
        return \Response::forge($this->formfieldset());
    }

    protected function formfieldset()
    {
        $fieldset =  \Fieldset::forge('form'); 
        $fieldset->add('title', 'title', array('maxlength' => 50), array(array('required')));
        return $fieldset;
    }

  • yes you 're right i didn't consider it  but  false is returned by var_dump still 
  • HarroHarro
    Accepted Answer
    With the code I gave you the var_dump() can't return false?
  • With your code var_dump must return fieldset object right ? But i tested it and return false
  • HarroHarro
    Accepted Answer
    It is impossible that $this->formfieldset() would ever return false.
  • at work i have windows and i've tested it repeatedly and it return me false now i am at home and i have ubuntu  i try to test it again and result was amazing it  return me fieldset object .
    tomorrow i will test it again.  
  • HarroHarro
    Accepted Answer
    Can't imagine that it has to do with the operating system version.

    The variable is created in

    $fieldset =  \Fieldset::forge('form');

    If that fails, the ->add() call on the next line with give you a PHP error (call on a non-object). If you don't have that error, then you have a valid fieldset object. Since it's not a notice, it will not silently continue...
  • its all done correctly now, i don't know what happened yesterday perhaps i didn't save that file , 
    btw thank you for all help mr verton :)
  • HarroHarro
    Accepted Answer
    You're welcome, happy coding! :-)

Howdy, Stranger!

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

In this Discussion