Welcome Guest, Not a member yet? Register   Sign In
View string needs escape if back slash on end
#11

For some reason if I use all forward / slashes CodeIgniter throws me the WHOOPS! Error

If I access it like below it works.

PHP Code:
echo view('Insitefx\Admin\Views/'.$slug); 

Namespaces use the back slash \

So my question is this the correct way of defining this url?

Or will this also cause problems?
What did you Try? What did you Get? What did you Expect?

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

IMO namespaces are *not* directories and should always be referenced with backslashes. What works versus what "should be" vary quite a bit, but in this case the slash directions don't really matter because `view()` uses FileLocator's `locateFile()` which has the following code:
PHP Code:
        // Standardize slashes to handle nested directories.
        
$file strtr($file'/''\\'); 

... which replaces all forward slashes with backslashes prior to executing the lookup. Bottom line is: backslash is more technically correct (opinion), either slash will work, backslash requires escaping.
Reply
#13

(07-04-2019, 08:30 AM)InsiteFX Wrote: For some reason if I use all forward / slashes CodeIgniter throws me the WHOOPS! Error

If I access it like below it works.

PHP Code:
echo view('Insitefx\Admin\Views/'.$slug); 

Namespaces use the back slash \

So my question is this the correct way of defining this url?

Or will this also cause problems?

That is a perfectly accurate way to do it. PHP and Composer (or any other auto-loader that I'm aware of in common use) do not provide the ability to auto-load any files other than classes. What we have provided is a way to "namespace" non-class files, like view files, helpers, etc. This makes code modules work for us and is pretty convenient in many instances, I think.

Technically, the first part of the filename is the namespace, which should use backslashes, i.e. Insitefx\Admin\Views. Anything after that is part of the file path and would typically use forward slashes, though is likely not necessary as MGatner points out. However, I do believe that in order for it to be recognized as having a namespace, you must have at least one backslash in the filename, otherwise it will attempt to interpret those as either folders within app/Views, or part of the filename itself.
Reply
#14

Thanks Lonnie,

The below work's just fine.

PHP Code:
echo view('Insitefx\Blog\Views/pages/'.$page$data); 

I think the User's Guide should be updated to include this so others do not get confused.
What did you Try? What did you Get? What did you Expect?

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

I realized in the shower this morning that I was being a little overly complicated with my reply actually. Since the namespace is expected to match to folders names for any portions below the main namespace, all you need is backslashes. That's simpler to remember and much less complex. Smile
Reply
#16

I have also the same issue when I followed the tutorial from the official documentation. After I create the Controller, Model and Views for "News" from the tutorial and tried to make it in module. And following the documentation and the above threads, the code for calling view seems quite confuse. Just want to know it there a better way to render the view when sub-directory view and module views are used in the Controller.

PHP Code:
        $data = [
            'news'  => $model->getNews(),
            'title' => 'News archive',
        ];

        echo view('templates/header'$data);
        echo view('News\Views\overview'$data);
        echo view('templates/footer'); 

Folder structure

   
Reply




Theme © iAndrew 2016 - Forum software by © MyBB