Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Routing not working in localhost and 1&1-webserver + other questions
  • // My text was too long, need to shorten it //
    1.Routing does not work. It calls every time my action_index, but i have a function named action_show, but from the routes 'about' => 'news/show' it isnt called, but instead index. and when i rename my root: '_root_'  => 'news/show' it calls nothing. .htacces modified, localhost modified, tried on webserver and local: nothing works. Someone has an idea what goes wrong?

    Sorry for short question :D

    image
  • Here some information

    My-Routes:
    return array(
    '_root_'  => 'news/index',  // The default route
    '_404_'   => 'welcome/404',    // The main 404 route
    'news/show' => 'news/show', // Show News
    );

    My Controller:

    class Controller_News extends Controller_Template{
        //Define the Layout for the whole controller
        public $template = 'layout/newsLayout';
        public function before()
        {
            parent::before();
        }
        public function after($response)
        {
            $response = parent::after($response);
            return $response;
        }

        //Get Components for template/theme.
        //They will be included in all actions to maintain easy adjustable content-boxes and prevents further declaration of the same script.
        private function getComponents() {
        $this->template->navbar = View::forge('components/navbar', Controller_Components::component_navbar());
        $this->template->header = View::forge('components/header', Controller_Components::component_header());
        $this->template->footer = View::forge('components/footer', Controller_Components::component_footer());
        $this->template->title = "hy";
        $this->template->sidebar = View::forge('components/sidebar', Controller_Components::component_sidebar());
        $this->template->cookieConsent = View::forge('components/cookieConsent', Controller_Components::component_cookieConsent(), false);
        }
        //The mainpage (index). To change the mainpage, go into app/config/routes and change the _root_ to the controller you want.
        public function action_index() {
            $this->getComponents();
            $this->template->content = View::forge('news/index', Controller_News_Index::index(), false);
        }
        //The show-action to display a single news
        public function action_show() {
            $this->getComponents();
            $this->template->content = View::forge('news/show', Controller_News_show::show(), false);
        }
    }
  • Here are the 2 .htaccess that i tried:
    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    </IfModule>
    # Multiple Environment config, set this to development, staging or production
    # SetEnv FUEL_ENV production

    <IfModule mod_rewrite.c>

    RewriteEngine on

    # NOTICE: If you get a 404 play with combinations of the following commented out lines
    #AllowOverride All
    #RewriteBase /wherever/fuel/is

    I had already changed above 2 options

        # Make sure directory listing is disabled
    Options +FollowSymLinks -Indexes

    # Restrict your site to only one domain
    # !important USE ONLY ONE OPTION

    # Option 1: To rewrite "www.domain.com -> domain.com" uncomment the following lines.
    #RewriteCond %{HTTPS} !=on
    #RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    #RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    # Option 2: To rewrite "domain.com -> www.domain.com" uncomment the following lines.
    #RewriteCond %{HTTPS} !=on
    #RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
    #RewriteCond %{HTTP_HOST} (.+)$ [NC]
    #RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

    # Remove index.php from URL
    #RewriteCond %{HTTP:X-Requested-With} !^XMLHttpRequest$
    #RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
    #RewriteRule ^index\.php(.*)$ $1 [R=301,NS,L]

        # make HTTP Basic Authentication work on php-fcgi installs
        <IfModule mod_fcgid.c>
            RewriteCond %{HTTP:Authorization} .
            RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
        </IfModule>

    # Send request via index.php if not a real file or directory
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # deal with php-fcgi first
    <IfModule mod_fcgid.c>
            RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
    </IfModule>

    # no php-fcgi, check for sapi and fpm
    <IfModule !mod_fcgid.c>

    # for PHP5 sapi installations
    <IfModule mod_php5.c>
    RewriteRule ^(.*)$ index.php/$1 [L]
    </IfModule>

    <IfModule !mod_php5.c>

    # for PHP7 sapi installations
    <IfModule mod_php7.c>
    RewriteRule ^(.*)$ index.php/$1 [L]
    </IfModule>

    # for fpm installations
    <IfModule !mod_php7.c>
    RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
    </IfModule>

    </IfModule>

    </IfModule>

    </IfModule>
  • Oke, different Questions: Why are namespaces not working? Im trying to use them, but i get the Error-Message, that 'model/MODEL was not found', use statements the same. So as an example: in my controller i say 'use /Model/news'; ', but i get an Error 'could not find class news in 'model/Model'. Where are the 2 model´s coming from?

    And: Do you have a better way to name the classes instead of theyre full name (like Controller_News_Index)? Im trying to separate the view-action from the controller-workload and give controller-specified functions to each subclass to prevent redeclaring each time the same function.

    I appreciate the help, and please, tell me if i do smth the wrong way or have a bad type of programming that does not work well with FuelPHP (or HMVC, or overall just bad habits). Mistakes are there for learning from them.

    With best regards,

    JH.
  • For next time, please one question per thread.

    Q1: Please var_dump $_SERVER at the top of your index.php and post the values of REQUEST_URI, QUERY_STRING, SCRIPT_NAME and PATH_INFO (if exists) here.

    Q2: Namespaces work with backslashes, not forward slashes. So "use \Model\News".

    As to the class names, the rules are simple:
    1 - all filenames need to be lower case
    2 - there is a one-to-one mapping between classname and filename
    3 - you can use underscores or namespaces, or mix the two
    4 - once you pick a name, don't use alternatives

    So, a class like "Controller_Some_Class_Name" in the global namespace is stored in app/classes/controller/some/class/name.php, But so is the class "Name" in the namespace "\Controller\Some\Class", or the class "Class_Name" in the namespace "\Controller\Some.

    Note that if you want to use the "Controller" namespace instead of the "Controller_" prefix, you need to enable this in the config.
  •  ["REQUEST_URI"]=> string(10) "/news/show" 
     ["QUERY_STRING"]=> string(0) ""
    ["SCRIPT_NAME"]=> string(10) "/index.php" 
    ["ORIG_PATH_INFO"]=> string(10) "/news/show"

    And some more Info, just for completion:

    ["REDIRECT_REDIRECT_SCRIPT_URI"]=> string(50) "[Domain hier]/news/show" 
    ["SCRIPT_NAME"]=> string(10) "/index.php" 
    ["STATUS"]=> string(3) "200" 
    ["ORIG_PATH_INFO"]=> string(10) "/news/show"
    ["ORIG_PATH_TRANSLATED"]=> string(67) "[Root hier]/public/index.php"
    ["PHP_SELF"]=> string(10) "/index.php" 

    Q2: I used Backslashes, i just wrote in "/" because it was too early to think it throught ^^ Sorry for that.

    Q3: I already understand the namespace-basics, the problem is that even with the correct namespaces and naming of my classes, if i try to use them and request my Models it would instead put in "use \Model\news\news;" to => "use \Model\MODEL;\ . namespace would be "namespace \Model;". Still same error. Or it could not find the file specified. Or if it would find the file, it would not load the class even with correct written namespace.

    And for the naming of classes: as you can see, i used Controller_News_Index::index() to give in the informations i need. The question was: can i rename it inside my script without writing __construct() {} and puttting a $this->class = new Controller_News_Index(); ? Otherwise its a pain in the back *hust* and not very clean.

    Well, thank you so far for your answer. I appreciate the help.

    And PS: If i use the original .htaccess, it complete gives up and prints out a 500 Server error (

    Internal Server Error etc. pp.).

    Only with the smaller one does it not break up.
    Then there was the problem with the timezone, but i managed it to fix it by manually adressing it in my config.php. So, well... Hope that helps.

    Funy fact: the original, on the other hand, works with localhost...

    PHP Version 5.6 to 7.0 geupdated, then to 7.1 ... nothing works. It shows me an 

    404 - Page not found!

    when i type in /news/show/fwaf . But when i use /news/show , its just like displaying the startpage ('_root_').

    I dont know, maybe the redirect is getting overwritten by smth? And i cant use the original version of the .htaccess because... yeah, i have already written that.
  • And here are the 2 functions i give to the view:

    //For root

    class Controller_News_Index extends Controller_News {
        public static function index() {
            $data = (object) array();
            $data->AllNews = Model_News::find('all');
            return $data;
        }
    }

    //For Show
    class Controller_News_Show extends Controller_News {
        
        public static function show() {
            $data = (object) array();
            $data->AllNews = Model_News::query()->where('id', '=', '1')->get_one()->to_array();
            return $data;
        }
    }

    //News-Model
    class Model_News extends ORM\Model{
        protected static $_table_name = 'news';
        protected static $_primary_key = array('id');
        protected static $_properties = array('id','title','message','category','rubrik','hasTeamReference','teamId','hasSquadReference','squadId','isActive','isRewritten','createdAt');
    }


    Just experimenting with the CRUD.

    Even changing view does nothing. The script always calls my 'index' instead of 'show'.
  • Timezone must always be set, if it is not set in the php.ini. it is a PHP requirement for date functions, which is why Fuel checks for it, and gives an error if it is not set.

    Your $_SERVER info looks fine, so that is not the problem. 

    What platform are you on? Windows? Linux? Something else? Which webserver (I assume Apache since you mention htaccess)? And what PHP setup? FPM? CGI? SAPI? 

    The htaccess supplied is tested on Apache in all combinations with PHP 5.x and 7.0. It has not been tested with 7.1, which may cause your issue, it might be the module tests fail. 

    I don't get what you want with Controller_News_Index. I see it being used as a parameter to View::forge(), which suggests it is not a controller at all, but a normal class used as a data provider. But you should be able to use it like that, the autoloader won't have any issue finding it, I just tested that here.
  • HarroHarro
    Accepted Answer
    Ah, wait a sec...

    Because you mis-use a controller as an internal data provider, you mess up the controller detection system.

    If you request "/news/show", this could mean:
    - Controller_News_Show::action_index()
    - Controller_News::action_show()
    - Controller_News::router()

    In this sequence. And because in your case, Controller_News_Show exists, it will load and call that, instead of Controller_News::action_show(), what you want it to load.

    So you have made a design error. Your data provider classes should be models in the strict MVC sense. Or create them in app/classes/provider, and call them Provider_News_Show or something. 

    Make sure that the system folders (controller/model/presenter) in classes only contain the classes that belong there.
  • Huh, wait a sec:

    So, i renamed my actions to the following:

        //The mainpage (index). To change the mainpage, go into app/config/routes and change the _root_ to the controller you want.
        public function action_hello() {
            $this->getComponents();
            $this->template->content = View::forge('news/index', Controller_News_Index::index(), false);
        }
        //The show-action to display a single news
        public function action_showNews() {
            $this->getComponents();
            $this->template->content = View::forge('news/index', Controller_News_Index::index(), false);
        }

    Now it uses my actions according to the following routes:

    <?php
    return array(
    '_root_'  => 'news/hello',  // The default route
    '_404_'   => 'welcome/404',    // The main 404 route
            '_500_'   => 'welcome/404',    // The main 404 route
    'news/show' => 'news/showNews', // Show News
    );

    When i delete my content from showNews now, it gives out that there are undefined variables... So that works... But _show after action_ does not work, and when i write _index, and i use _show in another action and try to call that, it uses the action_index and not action_show.... Well... thats unexpected... Do you have an answer to that?
  • HarroHarro
    Accepted Answer
    See my previous post. ;)
  • WUHU, I DESTROYED FUELPHP :D MOM, I MADE IT XDD

    Nah, jokes aside, well, most likely _show is messing it all up, and index is just flipping side with it. Most likely in the background it messes up everything you guys programmed. Damn, im good.

    Well, that likely was the cause of everything. That explains why everything else in that action didnt work either.

    So, im renaming all my actions with care, love and salt.... Much salt.
    image
  • It suggests the docs need more info about how the cascading filesystem works.

    Good you've got it sorted though.

Howdy, Stranger!

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

In this Discussion