CodeIgniter Forums
Question on include() in a module [newbie!!] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Question on include() in a module [newbie!!] (/showthread.php?tid=92644)



Question on include() in a module [newbie!!] - DarrenJ - 03-22-2025

Hi guys
I am just starting my Ci journey, and am following a tutorial from Udemy. Basic app is allow users to register and then create a blog article.
As extra "homework" I am adding functionality to edit a users name and email address. I have everything working but am hitting an issue with an include file and am after some guidance - I am sure I am missing something obvious here..
So, I have an Admin module, laid out thus:
Code:
Admin
-> Config
-> Database
-> Helpers
-> View
->->Users
->->->Edit.php
->->->form.php
->->->index.php
->->->show.php
app
->Config
etc...
The issue is is with the Edit, the code for which is:
Code:
<?= $this->extend('Layouts\default') ?>

<?= $this->section("title") ?>Edit User<?= $this->endSection() ?>

<?= $this->section("content") ?>

  <h1>Edit User</h1>

  <?php if (session()->has("errors")): ?>

      <ul>
        <?php foreach(session("errors") as $error): ?>
          <li><?= $error ?></li>
        <?php endforeach; ?> 
      </ul>
  <?php endif; ?>

  <?= form_open("admin/users/" . $user->id) ?>
 
    <input type="hidden" name="_method" value="PATCH">

    <?= $this->include("Admin\Views\Users\\form") ?>
    <!-- <label for="title">Email:</label>
    <input type="text" id="email" name="email" value="<?= old("title", esc($user->email)) ?>">
    <label for="content">First Name:</label>
    <input type="text" id="first_name" name="first_name" value="<?= old("title", esc($user->first_name)) ?>"> -->

    <button>Save</button>

  </form>

<?= $this->endSection() ?>
The issue is the line:

Code:
<?= $this->include("Admin\Views\Users\\form") ?>
As you can see, I have had to escape the name of the file in order to get it to be picked up - but this doesn't feel right to me.
I have played with adding APPPATH and ROOTPATH to the include path, but these don't work either.
The error I get "Invalid file" and I have tried various combinations of the Admin\Views\Users, with backslashes, forward slashes - all with the same error.
The code in the form.php file is in the code above, just commented out. If I uncomment it and comment out the include call, it all works.

Please help a newbie!

Thanks
Darren


RE: Question on include() in a module [newbie!!] - InsiteFX - 03-24-2025

What version of CodeIgniter are you using?


RE: Question on include() in a module [newbie!!] - DarrenJ - 03-25-2025

Ah sorry - 4.6


RE: Question on include() in a module [newbie!!] - kcs - 04-17-2025

Maybe I am confused with the structure of your app, but if the view calling the form is at the same level within our views, have you tried
Code:
<?= $this->include("/users/form") ?>



RE: Question on include() in a module [newbie!!] - captain-sensible - 04-17-2025

ive written a blog engine https://github.com/captain-sensible/CI4-CMS but it doesn't use shield i have have coded for one user.

But basically it runs just about out of the box since it uses sqlite for db and therefore no messing with MySQl , phpmyadnin

It uses sessions ,and checking if admin is logged in. Your welcome to fork


RE: Question on include() in a module [newbie!!] - grimpirate - 04-19-2025

Assuming you followed the appropriate autoloading namespacing as per here the code you wrote works as expected.
PHP Code:
<?= $this->include("Admin\Views\Users\\form"?>
Implies that you're escaping \V (is not an escape sequence) you're escaping \U (is not an escape sequence) and you're escaping \\ (is an escape sequence and is functionally equivalent in a double-quoted string to a single backslash). You should instead use single quoted strings:
PHP Code:
<?= $this->include('Admin\Views\Users\form'?>
Double quoted strings behave differently than single quoted strings when using backslash.