Calling routes from a view in CodeIgniter












0















I would like to avoid hard-coding urls in the views.



Example:



//view
echo form_open( base_url( 'users/add' ) );...

//routes
$route['users/add']['post'] = 'UserController/insert';


This way every time I update the url in routes, I have to go to view, find the form and manually update the url in view which can be very taxing.



In laravel you can name a route like this:



//routes.php
$route->post('users/add', 'UserController@insert')->name('insertUser');


and call it directly from view with a helper function



//view
form_open( routes('insertUser') );...


This way you the url in the view updates automatically, and saves you the trouble of doing it manually.



I'm wondering if there is something similar in CodeIgniter.



Thanks in advance!










share|improve this question



























    0















    I would like to avoid hard-coding urls in the views.



    Example:



    //view
    echo form_open( base_url( 'users/add' ) );...

    //routes
    $route['users/add']['post'] = 'UserController/insert';


    This way every time I update the url in routes, I have to go to view, find the form and manually update the url in view which can be very taxing.



    In laravel you can name a route like this:



    //routes.php
    $route->post('users/add', 'UserController@insert')->name('insertUser');


    and call it directly from view with a helper function



    //view
    form_open( routes('insertUser') );...


    This way you the url in the view updates automatically, and saves you the trouble of doing it manually.



    I'm wondering if there is something similar in CodeIgniter.



    Thanks in advance!










    share|improve this question

























      0












      0








      0








      I would like to avoid hard-coding urls in the views.



      Example:



      //view
      echo form_open( base_url( 'users/add' ) );...

      //routes
      $route['users/add']['post'] = 'UserController/insert';


      This way every time I update the url in routes, I have to go to view, find the form and manually update the url in view which can be very taxing.



      In laravel you can name a route like this:



      //routes.php
      $route->post('users/add', 'UserController@insert')->name('insertUser');


      and call it directly from view with a helper function



      //view
      form_open( routes('insertUser') );...


      This way you the url in the view updates automatically, and saves you the trouble of doing it manually.



      I'm wondering if there is something similar in CodeIgniter.



      Thanks in advance!










      share|improve this question














      I would like to avoid hard-coding urls in the views.



      Example:



      //view
      echo form_open( base_url( 'users/add' ) );...

      //routes
      $route['users/add']['post'] = 'UserController/insert';


      This way every time I update the url in routes, I have to go to view, find the form and manually update the url in view which can be very taxing.



      In laravel you can name a route like this:



      //routes.php
      $route->post('users/add', 'UserController@insert')->name('insertUser');


      and call it directly from view with a helper function



      //view
      form_open( routes('insertUser') );...


      This way you the url in the view updates automatically, and saves you the trouble of doing it manually.



      I'm wondering if there is something similar in CodeIgniter.



      Thanks in advance!







      php codeigniter url view routing






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 days ago









      failedCoderfailedCoder

      4019




      4019
























          2 Answers
          2






          active

          oldest

          votes


















          1














          The way that laravel implements it is simply by giving a name to the route which you can already do it yourself in codeingniter by using $config simply by creating a new config file in your app/config and name your route as you did in laravel like this:



          $config['insertUser'] = 'users/add';


          Then load that config file in your controller like this:



          $this->load->config('your_config_file_name');


          Then in your view you can use that value like this:



          form_open( base_url( $this->config->item('insertUser') ) );





          share|improve this answer































            1














            You could achieve it by first set a router config item within the config file ( the default file is located at application/config/config.php ), for example :



            $config['routes']['insertUser'] = 'users/add';


            then supply the config above into the standard routes item on routes.php,



            $route[$this->config->item('routes')['insertUser']]['post'] = 'UserController/insert';


            and then on the view you could call it dynamically like this :



            echo form_open( base_url( $this->config->item('routes')['insertUser'] ) );


            So each time if you have to change the route you just change on the config.php.






            share|improve this answer























              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%2f54252786%2fcalling-routes-from-a-view-in-codeigniter%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









              1














              The way that laravel implements it is simply by giving a name to the route which you can already do it yourself in codeingniter by using $config simply by creating a new config file in your app/config and name your route as you did in laravel like this:



              $config['insertUser'] = 'users/add';


              Then load that config file in your controller like this:



              $this->load->config('your_config_file_name');


              Then in your view you can use that value like this:



              form_open( base_url( $this->config->item('insertUser') ) );





              share|improve this answer




























                1














                The way that laravel implements it is simply by giving a name to the route which you can already do it yourself in codeingniter by using $config simply by creating a new config file in your app/config and name your route as you did in laravel like this:



                $config['insertUser'] = 'users/add';


                Then load that config file in your controller like this:



                $this->load->config('your_config_file_name');


                Then in your view you can use that value like this:



                form_open( base_url( $this->config->item('insertUser') ) );





                share|improve this answer


























                  1












                  1








                  1







                  The way that laravel implements it is simply by giving a name to the route which you can already do it yourself in codeingniter by using $config simply by creating a new config file in your app/config and name your route as you did in laravel like this:



                  $config['insertUser'] = 'users/add';


                  Then load that config file in your controller like this:



                  $this->load->config('your_config_file_name');


                  Then in your view you can use that value like this:



                  form_open( base_url( $this->config->item('insertUser') ) );





                  share|improve this answer













                  The way that laravel implements it is simply by giving a name to the route which you can already do it yourself in codeingniter by using $config simply by creating a new config file in your app/config and name your route as you did in laravel like this:



                  $config['insertUser'] = 'users/add';


                  Then load that config file in your controller like this:



                  $this->load->config('your_config_file_name');


                  Then in your view you can use that value like this:



                  form_open( base_url( $this->config->item('insertUser') ) );






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 days ago









                  Sherif SalahSherif Salah

                  1,0521516




                  1,0521516

























                      1














                      You could achieve it by first set a router config item within the config file ( the default file is located at application/config/config.php ), for example :



                      $config['routes']['insertUser'] = 'users/add';


                      then supply the config above into the standard routes item on routes.php,



                      $route[$this->config->item('routes')['insertUser']]['post'] = 'UserController/insert';


                      and then on the view you could call it dynamically like this :



                      echo form_open( base_url( $this->config->item('routes')['insertUser'] ) );


                      So each time if you have to change the route you just change on the config.php.






                      share|improve this answer




























                        1














                        You could achieve it by first set a router config item within the config file ( the default file is located at application/config/config.php ), for example :



                        $config['routes']['insertUser'] = 'users/add';


                        then supply the config above into the standard routes item on routes.php,



                        $route[$this->config->item('routes')['insertUser']]['post'] = 'UserController/insert';


                        and then on the view you could call it dynamically like this :



                        echo form_open( base_url( $this->config->item('routes')['insertUser'] ) );


                        So each time if you have to change the route you just change on the config.php.






                        share|improve this answer


























                          1












                          1








                          1







                          You could achieve it by first set a router config item within the config file ( the default file is located at application/config/config.php ), for example :



                          $config['routes']['insertUser'] = 'users/add';


                          then supply the config above into the standard routes item on routes.php,



                          $route[$this->config->item('routes')['insertUser']]['post'] = 'UserController/insert';


                          and then on the view you could call it dynamically like this :



                          echo form_open( base_url( $this->config->item('routes')['insertUser'] ) );


                          So each time if you have to change the route you just change on the config.php.






                          share|improve this answer













                          You could achieve it by first set a router config item within the config file ( the default file is located at application/config/config.php ), for example :



                          $config['routes']['insertUser'] = 'users/add';


                          then supply the config above into the standard routes item on routes.php,



                          $route[$this->config->item('routes')['insertUser']]['post'] = 'UserController/insert';


                          and then on the view you could call it dynamically like this :



                          echo form_open( base_url( $this->config->item('routes')['insertUser'] ) );


                          So each time if you have to change the route you just change on the config.php.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 2 days ago









                          Hasta DhanaHasta Dhana

                          1,6172413




                          1,6172413






























                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54252786%2fcalling-routes-from-a-view-in-codeigniter%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

                              Liquibase includeAll doesn't find base path

                              How to use setInterval in EJS file?

                              Petrus Granier-Deferre