Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Admin move it into a Module
  • I have created am Admin dashboard in the app folder that is using Ormauth with models users permissions roles etc; it is running fine in the app. I have search all over and most FuelPHP module example links are DEAD! Doe's some one have a nice tutorial of how to move this into a module?

    Even the FuelPHP doc's do not give a clear example of creating a module. A good example of a module like Admin Dashboard with Ormauth would be an asset to the FuelPHP documentation.

    Thank you Ray

  • You want to move the entire app folder, or just parts of it?

    A module has exactly the same structure as the app folder, they only difference is the namespace.

    So it also contains classes, views, etc If you move a controller, all you have to do is:
    - add the namespace if the controller didn't have one
    - or prefix an existing namespace with the module name

    And you need to check the code for all references to Fuel classes that are are not prefixed to global (i.e. don't have a leading backslash). You need to add that leading backslash.

    so

    class Controller_Test extends Controller
    {
        public function action_index()
        {
            return View::forge('test/index');
        }
    }

    becomes

    namespace Modulename;

    class Controller_Test extends \Controller
    {
        public function action_index()
        {
            return \View::forge('test/index');
        }
    }

    Job done.
  • p.s. it is good practice to always prefix your classes, even when you are already in the global namespace.

    It makes these moves a lot easier, and makes PHP a bit faster since it will not have to search namespaces for a match.
  • Hi Harro,

    I have already done that but it generates a redirect loop in the chrome browser.

    I autoloaded the admin module, and added the namespace.

    It may have something to do with the base controller etc; I created a base directory like the fuel depot.

    Or it has something to do with the Ormauth in the admin.

    Here is the directory structure in the app folder:
    app/classes/controller/admin
    -- fresh.php
    -- groups.php
    -- metadata.php
    -- pages.php
    -- permissions.php
    -- roles.php
    -- users.php

    app/classes/controller/base
    -- admin.php
    -- base.php
    -- public.php

    app/classes/model
    -- fresh,php
    -- group.php
    -- metadata.php
    -- page.php
    -- permission.php
    -- role.php
    -- user.php

    The redirect loop is when it tries to login to Ormauth, in the app everything is working fine view edit forms etc;
    I will seperate everything later into there own modules, first I want to get it to work.

  • Obviously, when you move the code into a module, by default the URI changes, so that means you'll have to look at redirects too.

    It's difficult to see from there where it goes wrong, but the Request class logs every request in development mode, so check your logs what is requested, and where it redirects to.

    Now that I can see your file structure, I see that you have all code in an admin folder. This makes moving it a bit easier:

    class Controller_Admin_Fresh extends Controller {}

    becomes

    namespace Admin;
    class Controller_Fresh extends \Controller {}

    by moving app/classes/controller/admin to app/modules/admin/classes/controller.

    In this case the URI will remain '/admin/fresh/index', and you should not have any (or at least a lot less) redirect issues.
  • Ok, I'll try to debug it more here.

    Do I need to do anything with the auth stuff in the Controller_Base? It's doing the Auth checks etc; In there

  • Hi Harro,

    Ok, here is what the log file is stating over and over again before the browser timeouts error setting in config is: .Fuel::L_ALL,

    Error: I'am using a route 'admin' => 'admin/admin/index',  - is this correct?

    INFO - 2014-06-09 16:07:00 --> Fuel\Core\Request::execute - Setting main Request
    INFO - 2014-06-09 16:07:02 --> Fuel\Core\Request::__construct - Creating a new main Request with URI = "admin"
    INFO - 2014-06-09 16:07:02 --> Fuel\Core\Request::execute - Called
    INFO - 2014-06-09 15:58:36 --> Fuel\Core\Request::execute - Setting main Request
    INFO - 2014-06-09 15:58:36 --> Fuel\Core\Request::__construct - Creating a new main Request with URI = "admin/login"
    INFO - 2014-06-09 15:58:36 --> Fuel\Core\Request::execute - Called

    This repeated like 20 times in the log file.

  • admin/admin/index maps to

    namespace Admin;
    class Controller_Admin
    {
        public function action_index()
        {
        }
    }

    in app/modules/admin/classes/controller/admin.php.

    Is that correct?

    Then it redirects to "admin/login", which should then be in

    namespace Admin;
    class Controller_Login
    {
        public function action_index()
        {
        }
    }

    in app/modules/admin/classes/controller/login.php.

    And the question is if this is ever loaded. You might have a routing issue causing the loop.
  • Hi Harro,

    There is no Controller_Login, the login is in the Controller_Admin as action_login this was generated by the oil utility.

    But I found out that this is were it is looping in the action_login:

        // Already logged in
        \Auth::check() and \Response::redirect('admin');

    It never gets pass here looks like the \Auth::check() is not working and so it keeps redirecting back to the Controller_Admin.

  • You are sure you're on the latest Fuel version? This sounds like an old bug with the admin generation template.

    See https://github.com/fuel/oil/blob/1.8/develop/views/admin/orm/controllers/admin.php
  • Hi Harro,

    Yes I am on the newest version of 1.8 dev I update it every morning.

    And yes that is what my controller code is right to a tee.

    Please let me know if I' am doing something wrong here.

    pastebin:



  • Line 97 is primarily there to avoid loops. Since you have moved the controller to a namespace, check if \Request::active()->controller still contains "Controller_Admin", and not "Admin\Controller_Admin" which makes the if fail...
  •     // Check to see if user is logged in
        if (\Request::active()->controller !== 'Controller_Admin' or ! in_array(\Request::active()->action, array('login', 'logout')))

    echos Admin\Controller_Admin


    public function action_login()
    {
    // Already logged in
    \Auth::check() and \Response::redirect('admin');

  • Thought so, so you have to change it to

    if
    (\Request::active()->controller !== 'Admin\Controller_Admin' or !
    in_array(\Request::active()->action, array('login', 'logout')))

  • Thank you very much Harro, It is working like a charm now.

    I had to modify the template menu to work with the modules directory etc;

    I need routes for all the controllers, is it best to at a routes.php in the modules config directory?

    When I get this all finished I will create a pdf for it so others can understand it better, like how I created the models etc; with oil.


  • HarroHarro
    Accepted Answer
    Depends on what you want to route.

    Module route are required to have the primary segment as the module name. Meaning all your module routes should start with 'admin/...' otherwise they can not be used.

    So you can use module routes to route from the module to elsewhere, you can not use module routes to route from elsewhere (simply because the routes are only loaded when it's already determined it's a module request).

    In general, I try to avoid routes, and try to design my controller is a structured and logical way. Since most of the URI's are never typed in by users, it doesn't really matter what they are. Only URI's used as application "entry" may benefit from shorter URI. It is also a lot more flexible towards development, not having tons of hardcoded routes...

Howdy, Stranger!

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

In this Discussion