Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Confusion for using namespace
  • i see some coding with a symbol "\" inserting front class name , another is none .
    What's the difference ?
    tks!

    ex :
    \Request::forge('mycar') vs Request::forge('mycar')
    or
    class Welcome extends \Model vs class Welcome extends Model<br>
  • It is the same as with directories in a file system:

    "cd /this/that" is a fully qualified path from the root, "cd this/that" is relative to the current directory.

    If you are in the global namespace (i.e. your php file doesn't have a namespace specified), "\Request" is the same as "Request" (compare with the directory example when the current directory is the root).

    It is good practice to always prefix or use "use" statements, so you can't make mistakes when you start using namespaces, for example in modules.
  • I understand what you mean.

    for example , if mycontroller lives in a namespace, i have to prefix "Controller" with a "\", since it needs to be
    loaded from the global namespace.
    namespace Controller;

    class Example extends \Controller
    {
    }


    tks!
  • HarroHarro
    Accepted Answer
    Correct.

    And to prevent mistakes, it's good practice to just always prefix, even if your controller doesn't live in a namespace.
  • i test namespace but php return a networkerror: 404 Not Found - http://localhost/fuelphp/1015/public/

    //controller api
    namespace Controller;

    class Api extends \Controller
    {
        public function action_index()
        {       
            return \Response::forge(\View::forge('wcar/show'));
        }
    }

    since removing namespace it works fine .

    //controller api
    class Controller_Api extends Controller
    {
        public function action_index()
        {       
            return Response::forge(View::forge('wcar/show'));
        }
    }

    what's wrong ?
    tks!
  • HarroHarro
    Accepted Answer
    It you decide to namespace your controllers, you need to change the prefix in app/config/config.php from "Controller_" to "Controller\\", as documented here: http://fuelphp.com/docs/general/controllers/base.html#/controller_namespacing

    Otherwise the Request class doesn't know it needs to look for \Controller\Api instead of Controller_Api.
  • i see .when i change this setting it works fine.

    is it possible that namespace and none namespace exist at the same time in my fuelphp system ?
    example, one of packages uses namespace and another is no , or one of modules uses namespace and another is no .

    tks !
  • HarroHarro
    Accepted Answer
    Packages and modules are always in a namespace (equal to the name of the package or the module).

    Packages can't have controllers, for module controllers the same applies. Either you use "Controller_", or "\\Controller\\", but you can't mix both in a single application (as the config setting is global).

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion