How to set up CodeIgniter controllers for similar forms in different directories












1














I'm using CodeIgniter to access a variety of form types



I have a directory such as this:



-views
--resources
---app1
----form.php
---app2
----form.php
---app3
----form.php
---app4
----form.php


My class is currently very basic, but this



class Resources extends CI_Controller {

public function app1($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter

$this->load->view('templates/header', $data);
$this->load->view('resources/app1/form.php', $data);
$this->load->view('templates/footer', $data);


}

public function app2($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter

$this->load->view('templates/header', $data);
$this->load->view('resources/app2/form.php', $data);
$this->load->view('templates/footer', $data);


}

}


This seems very verbose and unnecessary to have a method for every form. However I can't find how I can change the directory without creating a new method. I would ideally like a method where I can pass in a new directory as an arg like $page can be. Eg:



class Resources extends CI_Controller {

public function view($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter

$this->load->view('templates/header', $data);
$this->load->view('resources/'. $folder. '/form.php', $data);
$this->load->view('templates/footer', $data);


}

}


However, it seems CodeIgniter doesn't allow this. Can anyone suggest a way in which this can work?










share|improve this question



























    1














    I'm using CodeIgniter to access a variety of form types



    I have a directory such as this:



    -views
    --resources
    ---app1
    ----form.php
    ---app2
    ----form.php
    ---app3
    ----form.php
    ---app4
    ----form.php


    My class is currently very basic, but this



    class Resources extends CI_Controller {

    public function app1($page = '')
    {
    $data['title'] = ucfirst($folder); // Capitalize the first letter

    $this->load->view('templates/header', $data);
    $this->load->view('resources/app1/form.php', $data);
    $this->load->view('templates/footer', $data);


    }

    public function app2($page = '')
    {
    $data['title'] = ucfirst($folder); // Capitalize the first letter

    $this->load->view('templates/header', $data);
    $this->load->view('resources/app2/form.php', $data);
    $this->load->view('templates/footer', $data);


    }

    }


    This seems very verbose and unnecessary to have a method for every form. However I can't find how I can change the directory without creating a new method. I would ideally like a method where I can pass in a new directory as an arg like $page can be. Eg:



    class Resources extends CI_Controller {

    public function view($page = '')
    {
    $data['title'] = ucfirst($folder); // Capitalize the first letter

    $this->load->view('templates/header', $data);
    $this->load->view('resources/'. $folder. '/form.php', $data);
    $this->load->view('templates/footer', $data);


    }

    }


    However, it seems CodeIgniter doesn't allow this. Can anyone suggest a way in which this can work?










    share|improve this question

























      1












      1








      1







      I'm using CodeIgniter to access a variety of form types



      I have a directory such as this:



      -views
      --resources
      ---app1
      ----form.php
      ---app2
      ----form.php
      ---app3
      ----form.php
      ---app4
      ----form.php


      My class is currently very basic, but this



      class Resources extends CI_Controller {

      public function app1($page = '')
      {
      $data['title'] = ucfirst($folder); // Capitalize the first letter

      $this->load->view('templates/header', $data);
      $this->load->view('resources/app1/form.php', $data);
      $this->load->view('templates/footer', $data);


      }

      public function app2($page = '')
      {
      $data['title'] = ucfirst($folder); // Capitalize the first letter

      $this->load->view('templates/header', $data);
      $this->load->view('resources/app2/form.php', $data);
      $this->load->view('templates/footer', $data);


      }

      }


      This seems very verbose and unnecessary to have a method for every form. However I can't find how I can change the directory without creating a new method. I would ideally like a method where I can pass in a new directory as an arg like $page can be. Eg:



      class Resources extends CI_Controller {

      public function view($page = '')
      {
      $data['title'] = ucfirst($folder); // Capitalize the first letter

      $this->load->view('templates/header', $data);
      $this->load->view('resources/'. $folder. '/form.php', $data);
      $this->load->view('templates/footer', $data);


      }

      }


      However, it seems CodeIgniter doesn't allow this. Can anyone suggest a way in which this can work?










      share|improve this question













      I'm using CodeIgniter to access a variety of form types



      I have a directory such as this:



      -views
      --resources
      ---app1
      ----form.php
      ---app2
      ----form.php
      ---app3
      ----form.php
      ---app4
      ----form.php


      My class is currently very basic, but this



      class Resources extends CI_Controller {

      public function app1($page = '')
      {
      $data['title'] = ucfirst($folder); // Capitalize the first letter

      $this->load->view('templates/header', $data);
      $this->load->view('resources/app1/form.php', $data);
      $this->load->view('templates/footer', $data);


      }

      public function app2($page = '')
      {
      $data['title'] = ucfirst($folder); // Capitalize the first letter

      $this->load->view('templates/header', $data);
      $this->load->view('resources/app2/form.php', $data);
      $this->load->view('templates/footer', $data);


      }

      }


      This seems very verbose and unnecessary to have a method for every form. However I can't find how I can change the directory without creating a new method. I would ideally like a method where I can pass in a new directory as an arg like $page can be. Eg:



      class Resources extends CI_Controller {

      public function view($page = '')
      {
      $data['title'] = ucfirst($folder); // Capitalize the first letter

      $this->load->view('templates/header', $data);
      $this->load->view('resources/'. $folder. '/form.php', $data);
      $this->load->view('templates/footer', $data);


      }

      }


      However, it seems CodeIgniter doesn't allow this. Can anyone suggest a way in which this can work?







      php codeigniter






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 20:44









      prikkles

      365




      365
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Calling your function "view" is almost certainly a bad idea... it's used by CI for the $this->load->view() for starters.



              public function app_form($page = '')
          {
          $data['title'] = ucfirst($folder); // Capitalize the first letter

          $this->load->view('templates/header', $data);
          $this->load->view('resources/'. $page. '/form.php', $data);
          $this->load->view('templates/footer', $data);

          }


          That should work but how are you going to call the functions? Via the routes.php file?






          share|improve this answer





















          • That's an oversight. I just wrote that as an example of a method I'm looking to have - I wouldn't call in view in my app.
            – prikkles
            Nov 25 at 20:53



















          0














          Actually you can.



          Create a base_controller inside your core folder and call it MY_Controller.php and make it extends CI_Controller and create a method inside MY_Controller and name it render, render_view, view whatever you want and inside that function load you layout partials and template and just pass the view to it: application/core/MY_Controller.php



          class MY_Controller extends CI_Controller {

          protected $data = array();

          public function render_view($view = '')
          {
          $this->load->view('templates/header', $this->data);
          $this->load->view('view_path/'. $view, $this->data);
          $this->load->view('templates/footer', $this->data);
          }
          }


          and for every controller in your application make it extend MY_Controller and whenever you wanna render a view use render_view($view) and you got you header and footer preloaded, and that's the simplest way of making it DRY.



          Finally in your controller it should be like this:



          class Resources extends CI_Controller {

          public function app1($page = '')
          {
          // $data array in my_controller, it will automatically be passed inside render_view
          $this->data['title'] = ucfirst($folder); // Capitalize the first letter
          $this->render_view('app1/form');
          }

          public function app2($page = '')
          {
          $this->data['title'] = ucfirst($folder); // Capitalize the first letter
          $this->render_view('app2/form');
          }


          }






          share|improve this answer





















          • Thanks for this - I'm going to use this so I don't have to load header and footer in every method. However, I'm looking for a way to load all the forms using one method. If I use a method per form, my methods could be unlimited (well into the hundreds) and that seems unnecessary when I just need to change the directory of the form in the method when loading the view.
            – prikkles
            Nov 26 at 6:57












          • You can achieve that here, i'm doing that myself .. i don't even have to use render_view unless i'm changing the template, but it takes a lot more code inside base controller to achieve that but i'll give you a hint, you can render the view based on the controller name represents view dir name and method name represents view or form name
            – Sherif Salah
            Nov 26 at 11:13











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437784%2fhow-to-set-up-codeigniter-controllers-for-similar-forms-in-different-directories%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Calling your function "view" is almost certainly a bad idea... it's used by CI for the $this->load->view() for starters.



              public function app_form($page = '')
          {
          $data['title'] = ucfirst($folder); // Capitalize the first letter

          $this->load->view('templates/header', $data);
          $this->load->view('resources/'. $page. '/form.php', $data);
          $this->load->view('templates/footer', $data);

          }


          That should work but how are you going to call the functions? Via the routes.php file?






          share|improve this answer





















          • That's an oversight. I just wrote that as an example of a method I'm looking to have - I wouldn't call in view in my app.
            – prikkles
            Nov 25 at 20:53
















          0














          Calling your function "view" is almost certainly a bad idea... it's used by CI for the $this->load->view() for starters.



              public function app_form($page = '')
          {
          $data['title'] = ucfirst($folder); // Capitalize the first letter

          $this->load->view('templates/header', $data);
          $this->load->view('resources/'. $page. '/form.php', $data);
          $this->load->view('templates/footer', $data);

          }


          That should work but how are you going to call the functions? Via the routes.php file?






          share|improve this answer





















          • That's an oversight. I just wrote that as an example of a method I'm looking to have - I wouldn't call in view in my app.
            – prikkles
            Nov 25 at 20:53














          0












          0








          0






          Calling your function "view" is almost certainly a bad idea... it's used by CI for the $this->load->view() for starters.



              public function app_form($page = '')
          {
          $data['title'] = ucfirst($folder); // Capitalize the first letter

          $this->load->view('templates/header', $data);
          $this->load->view('resources/'. $page. '/form.php', $data);
          $this->load->view('templates/footer', $data);

          }


          That should work but how are you going to call the functions? Via the routes.php file?






          share|improve this answer












          Calling your function "view" is almost certainly a bad idea... it's used by CI for the $this->load->view() for starters.



              public function app_form($page = '')
          {
          $data['title'] = ucfirst($folder); // Capitalize the first letter

          $this->load->view('templates/header', $data);
          $this->load->view('resources/'. $page. '/form.php', $data);
          $this->load->view('templates/footer', $data);

          }


          That should work but how are you going to call the functions? Via the routes.php file?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 20:58









          AlunR

          425310




          425310












          • That's an oversight. I just wrote that as an example of a method I'm looking to have - I wouldn't call in view in my app.
            – prikkles
            Nov 25 at 20:53


















          • That's an oversight. I just wrote that as an example of a method I'm looking to have - I wouldn't call in view in my app.
            – prikkles
            Nov 25 at 20:53
















          That's an oversight. I just wrote that as an example of a method I'm looking to have - I wouldn't call in view in my app.
          – prikkles
          Nov 25 at 20:53




          That's an oversight. I just wrote that as an example of a method I'm looking to have - I wouldn't call in view in my app.
          – prikkles
          Nov 25 at 20:53













          0














          Actually you can.



          Create a base_controller inside your core folder and call it MY_Controller.php and make it extends CI_Controller and create a method inside MY_Controller and name it render, render_view, view whatever you want and inside that function load you layout partials and template and just pass the view to it: application/core/MY_Controller.php



          class MY_Controller extends CI_Controller {

          protected $data = array();

          public function render_view($view = '')
          {
          $this->load->view('templates/header', $this->data);
          $this->load->view('view_path/'. $view, $this->data);
          $this->load->view('templates/footer', $this->data);
          }
          }


          and for every controller in your application make it extend MY_Controller and whenever you wanna render a view use render_view($view) and you got you header and footer preloaded, and that's the simplest way of making it DRY.



          Finally in your controller it should be like this:



          class Resources extends CI_Controller {

          public function app1($page = '')
          {
          // $data array in my_controller, it will automatically be passed inside render_view
          $this->data['title'] = ucfirst($folder); // Capitalize the first letter
          $this->render_view('app1/form');
          }

          public function app2($page = '')
          {
          $this->data['title'] = ucfirst($folder); // Capitalize the first letter
          $this->render_view('app2/form');
          }


          }






          share|improve this answer





















          • Thanks for this - I'm going to use this so I don't have to load header and footer in every method. However, I'm looking for a way to load all the forms using one method. If I use a method per form, my methods could be unlimited (well into the hundreds) and that seems unnecessary when I just need to change the directory of the form in the method when loading the view.
            – prikkles
            Nov 26 at 6:57












          • You can achieve that here, i'm doing that myself .. i don't even have to use render_view unless i'm changing the template, but it takes a lot more code inside base controller to achieve that but i'll give you a hint, you can render the view based on the controller name represents view dir name and method name represents view or form name
            – Sherif Salah
            Nov 26 at 11:13
















          0














          Actually you can.



          Create a base_controller inside your core folder and call it MY_Controller.php and make it extends CI_Controller and create a method inside MY_Controller and name it render, render_view, view whatever you want and inside that function load you layout partials and template and just pass the view to it: application/core/MY_Controller.php



          class MY_Controller extends CI_Controller {

          protected $data = array();

          public function render_view($view = '')
          {
          $this->load->view('templates/header', $this->data);
          $this->load->view('view_path/'. $view, $this->data);
          $this->load->view('templates/footer', $this->data);
          }
          }


          and for every controller in your application make it extend MY_Controller and whenever you wanna render a view use render_view($view) and you got you header and footer preloaded, and that's the simplest way of making it DRY.



          Finally in your controller it should be like this:



          class Resources extends CI_Controller {

          public function app1($page = '')
          {
          // $data array in my_controller, it will automatically be passed inside render_view
          $this->data['title'] = ucfirst($folder); // Capitalize the first letter
          $this->render_view('app1/form');
          }

          public function app2($page = '')
          {
          $this->data['title'] = ucfirst($folder); // Capitalize the first letter
          $this->render_view('app2/form');
          }


          }






          share|improve this answer





















          • Thanks for this - I'm going to use this so I don't have to load header and footer in every method. However, I'm looking for a way to load all the forms using one method. If I use a method per form, my methods could be unlimited (well into the hundreds) and that seems unnecessary when I just need to change the directory of the form in the method when loading the view.
            – prikkles
            Nov 26 at 6:57












          • You can achieve that here, i'm doing that myself .. i don't even have to use render_view unless i'm changing the template, but it takes a lot more code inside base controller to achieve that but i'll give you a hint, you can render the view based on the controller name represents view dir name and method name represents view or form name
            – Sherif Salah
            Nov 26 at 11:13














          0












          0








          0






          Actually you can.



          Create a base_controller inside your core folder and call it MY_Controller.php and make it extends CI_Controller and create a method inside MY_Controller and name it render, render_view, view whatever you want and inside that function load you layout partials and template and just pass the view to it: application/core/MY_Controller.php



          class MY_Controller extends CI_Controller {

          protected $data = array();

          public function render_view($view = '')
          {
          $this->load->view('templates/header', $this->data);
          $this->load->view('view_path/'. $view, $this->data);
          $this->load->view('templates/footer', $this->data);
          }
          }


          and for every controller in your application make it extend MY_Controller and whenever you wanna render a view use render_view($view) and you got you header and footer preloaded, and that's the simplest way of making it DRY.



          Finally in your controller it should be like this:



          class Resources extends CI_Controller {

          public function app1($page = '')
          {
          // $data array in my_controller, it will automatically be passed inside render_view
          $this->data['title'] = ucfirst($folder); // Capitalize the first letter
          $this->render_view('app1/form');
          }

          public function app2($page = '')
          {
          $this->data['title'] = ucfirst($folder); // Capitalize the first letter
          $this->render_view('app2/form');
          }


          }






          share|improve this answer












          Actually you can.



          Create a base_controller inside your core folder and call it MY_Controller.php and make it extends CI_Controller and create a method inside MY_Controller and name it render, render_view, view whatever you want and inside that function load you layout partials and template and just pass the view to it: application/core/MY_Controller.php



          class MY_Controller extends CI_Controller {

          protected $data = array();

          public function render_view($view = '')
          {
          $this->load->view('templates/header', $this->data);
          $this->load->view('view_path/'. $view, $this->data);
          $this->load->view('templates/footer', $this->data);
          }
          }


          and for every controller in your application make it extend MY_Controller and whenever you wanna render a view use render_view($view) and you got you header and footer preloaded, and that's the simplest way of making it DRY.



          Finally in your controller it should be like this:



          class Resources extends CI_Controller {

          public function app1($page = '')
          {
          // $data array in my_controller, it will automatically be passed inside render_view
          $this->data['title'] = ucfirst($folder); // Capitalize the first letter
          $this->render_view('app1/form');
          }

          public function app2($page = '')
          {
          $this->data['title'] = ucfirst($folder); // Capitalize the first letter
          $this->render_view('app2/form');
          }


          }







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 at 13:15









          Sherif Salah

          631512




          631512












          • Thanks for this - I'm going to use this so I don't have to load header and footer in every method. However, I'm looking for a way to load all the forms using one method. If I use a method per form, my methods could be unlimited (well into the hundreds) and that seems unnecessary when I just need to change the directory of the form in the method when loading the view.
            – prikkles
            Nov 26 at 6:57












          • You can achieve that here, i'm doing that myself .. i don't even have to use render_view unless i'm changing the template, but it takes a lot more code inside base controller to achieve that but i'll give you a hint, you can render the view based on the controller name represents view dir name and method name represents view or form name
            – Sherif Salah
            Nov 26 at 11:13


















          • Thanks for this - I'm going to use this so I don't have to load header and footer in every method. However, I'm looking for a way to load all the forms using one method. If I use a method per form, my methods could be unlimited (well into the hundreds) and that seems unnecessary when I just need to change the directory of the form in the method when loading the view.
            – prikkles
            Nov 26 at 6:57












          • You can achieve that here, i'm doing that myself .. i don't even have to use render_view unless i'm changing the template, but it takes a lot more code inside base controller to achieve that but i'll give you a hint, you can render the view based on the controller name represents view dir name and method name represents view or form name
            – Sherif Salah
            Nov 26 at 11:13
















          Thanks for this - I'm going to use this so I don't have to load header and footer in every method. However, I'm looking for a way to load all the forms using one method. If I use a method per form, my methods could be unlimited (well into the hundreds) and that seems unnecessary when I just need to change the directory of the form in the method when loading the view.
          – prikkles
          Nov 26 at 6:57






          Thanks for this - I'm going to use this so I don't have to load header and footer in every method. However, I'm looking for a way to load all the forms using one method. If I use a method per form, my methods could be unlimited (well into the hundreds) and that seems unnecessary when I just need to change the directory of the form in the method when loading the view.
          – prikkles
          Nov 26 at 6:57














          You can achieve that here, i'm doing that myself .. i don't even have to use render_view unless i'm changing the template, but it takes a lot more code inside base controller to achieve that but i'll give you a hint, you can render the view based on the controller name represents view dir name and method name represents view or form name
          – Sherif Salah
          Nov 26 at 11:13




          You can achieve that here, i'm doing that myself .. i don't even have to use render_view unless i'm changing the template, but it takes a lot more code inside base controller to achieve that but i'll give you a hint, you can render the view based on the controller name represents view dir name and method name represents view or form name
          – Sherif Salah
          Nov 26 at 11:13


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437784%2fhow-to-set-up-codeigniter-controllers-for-similar-forms-in-different-directories%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)