Welcome Guest, Not a member yet? Register   Sign In
APP_NAMESPACE do nothing..
#1

(This post was last modified: 06-28-2016, 06:17 AM by sv3tli0.)

Hi
I notice that there is no Namespace at the Home controller which is strange and I hope that it is just a temporary thing..
I tested the constant APP_NAMESPACE, but it doesn't do anything or at least I can't figure it out what its doing..

In my case I set APP_NAMESPACE to be "Testapp".
When I list all declared classes inside the controller Home .. 

Home exists only as \Home with no namespace, as its declared at first place.
And the APP_NAMESPACE Testapp is not listed at all inside the declared class list.

At the same time if I try to declare the App Namespace inside the controller everything dies..


What is the idea of that APP_NAMESPACE?
And why App/Controller/Home.php doesn't have any Namespace declaration ?
Best VPS Hosting : Digital Ocean
Reply
#2

I found similar case. The controller fail to find model when added namespace App\Model;
Reply
#3

I would also like to know about this issue as namespaces are very important in a modern framework design. *bump*
Reply
#4

CodeIgniter provides two methodologies you can use - the traditional, non-namespaced, controllers that are found by the folder, and now having namespace usage built-in. The Home controller is not namespaced to make it a little more familiar to those coming from previous versions of CodeIgniter. With namespaces, remember you have to declare them at the top of the file. So, if you want Home controller to be namespaced and available under the App\Controllers namespace you'll need to declare that:

Code:
<?php namespace App\Controllers;

class Home extends \CodeIgniter\Controller
{
    . . .
}

The APP_NAMESPACE constant is used to allow the application folder's namespace to be changed easily. However, that still means you have to declare the correct namespace within each class in the application folder. This is used within application/Config/Autoload.php to define the application folder as whatever namespace you've set it be. Which means controllers are namespaced to App\Controllers, models to App\Models, etc.

One caveat if you change the app's namespace - you might need to update the defaultNamespace setting in the routes file to match. The autoloader will first attempt to find a class matching the default namespace and the class name mentioned in the router, so App\Controllers\Home in this case. If it can't find that one, it looks for \Home (with no namespace).
Reply
#5

(06-28-2016, 02:58 AM)titounnes Wrote: I found similar case. The controller fail to find model when added namespace App\Model;

The sub-namespace must match the folder name, and you're missing an 's' on Model here. It should be App\Models.
Reply
#6
Thumbs Up 

(06-28-2016, 06:24 AM)kilishan Wrote:
(06-28-2016, 02:58 AM)titounnes Wrote: I found similar case. The controller fail to find model when added namespace App\Model;

The sub-namespace must match the folder name, and you're missing an 's' on Model here. It should be App\Models.

thanks. Your explanation is clear for me.
Reply
#7

(This post was last modified: 06-28-2016, 11:02 AM by sv3tli0.)

I fixed my mistakes and its ok.
Perhaps the default setup for the app should be at the same app namespace as it is defined at the constant.

And if there is no namespace the constant APP_NAMESPACE should be false or null..

Its not logical to set default namespace for the app but at the same time your controllers to be without namespaces..
This way you can have 1 Controller without namespace and 1 with App\Controllers namespace at the same time at the same folder working together..
And this sounds as mess..


P.S > A big problem for the NONE NAMESPACE case will be Same Controller/Model classes ..
At CI 3 to fix that there were suffixes but inside the current version there are no suffixes and it will be a problem I believe ..
Best VPS Hosting : Digital Ocean
Reply
#8

(This post was last modified: 06-28-2016, 11:31 AM by kilishan.)

@sv3tli0 - That makes sense to default it to having the Home controller namespaced. My original thinking was to default it to traditional CI3 methods where you don't have to namespace at all, but it might make more sense to default to using a namespace to get users in the habit.

I don't APP_NAMESPACE should always have a value, though, even if you don't use it, and stick to traditional patterns, in my opinion. Remember, there are two ways to use it (namespaced or traditional) and settings for one won't always make sense for the other, but that doesn't mean we want to take away the option from the other. Heck, they could mix it they wanted to, though the only time I'd recommend that is during a gradual transition from CI3.
Reply
#9

I've set these defaults in the repo now. It helped me find another small bug with routing, too, so thanks.
Reply
#10

(06-28-2016, 11:28 AM)kilishan Wrote: @sv3tli0 - That makes sense to default it to having the Home controller namespaced. My original thinking was to default it to traditional CI3 methods where you don't have to namespace at all, but it might make more sense to default to using a namespace to get users in the habit.

Hi,

I am fairly new to CI and loving it, and brand new to the forums but I just wanted to add my thoughts on this.

I think it's a great idea to use the namespaces by default, wherever possible. It encourages good developer practice and helps keep CI up-to-date with modern 'best' practices, plus it encourages those who like the traditional methods, or those who don't 'get' namespaces to move on to the newer way of doing things because they have the examples right there in front of them.

And for those who wish to disregard namespaces and use the traditional methods as long as it requires little reconfiguration on their part it's a win.

Just my tuppence worth Smile

TC
Reply




Theme © iAndrew 2016 - Forum software by © MyBB