Welcome Guest, Not a member yet? Register   Sign In
Question on include() in a module [newbie!!]
#1

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
Reply
#2

What version of CodeIgniter are you using?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Ah sorry - 4.6
Reply
#4

(This post was last modified: 04-17-2025, 01:56 AM by kcs.)

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") ?>
Reply
#5

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
CMS CI4 A CMS system, runs out of the box written on top of CI4
Arch Book  CodeIgniter4 on Apache(pages 92-114) 
Reply
#6

(This post was last modified: 04-19-2025, 09:17 PM by grimpirate.)

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB