• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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, 09: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.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
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
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.