Routing, Data Mapper and Illegal offset

64 views
Skip to first unread message

Dwaine Halberg

unread,
Sep 2, 2017, 2:27:16 AM9/2/17
to Fat-Free Framework
Hello all, I'm new to F3 (v3.6) and right now it's killing me! and I'm sure it's a simple fix but I just can't see it! I know you've been there!

server: apache 2.4.25
php 7.1.6
mariadb 10.2.6

I've gone through the youtube videos by taksmark and I'm testing to move forward, I'm taking data which I have from my exiting cms and trying to access with F3 v3.6 on a local windows 10 wamp.net server with htaccess working, my old app routes fine, but I want to get away from from my old code base and use F3.

Where my issues lies is if I access the home page data using a full page template.htm using the data mapper I can return and display all the data fine, but when I try to return the data from another row in the same table using a link (the 1st is static call to the row) I either get a page not found error, or illegal offset for a column in the table.


the controller code for the 2 methods:

class PageController extends WMS_Front_Controller {

   
function render($f3)
   
{    
        $pages
= new PageModel($this->db);
        $msg
= $pages->getById(1)[0];
       
        $f3
->set('row', $msg);
        echo
\Template::instance()->render('template.htm');
   
}
   
function pageDisplay($f3)
   
{
        $messages
= new PageModel($this->db);
        $f3
->set('wms_pages', $messages->getById($id)[0]);
        $f3
->set('row', 'main.htm');
        $template
= new Template;
       
echo \Template::instance()->render('site.htm');
   
}
}

the methods from the Page Model:
class PageModel extends DB\SQL\Mapper{
   
    public function __construct(DB\SQL $db){
        parent::__construct($db, 'wms_pages');
    }
  
    public function getById($id){
        $this->load(array('id=?',$id));
        return $this->query;
    }
}


The portion of the template I am outputting to (same in both templates):
        <!-- Page Content -->
        <div class="container">
   
            <!-- Page Heading/Breadcrumbs -->
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header">{{ @row.page_name }}</h1>
                </div>
            </div>
            <!-- /.row -->
   
            <!-- Content Row -->
            <div class="row">
                <div class="col-lg-12">
                    {{ @row.page_txt_1 | raw }} {{ @row.page_txt_2 | raw }}
                </div>
            </div>
            <!-- /.row -->

this is what i get if my href link is /example-page

Internal Server Error

Illegal string offset 'page_name'

[F:/wmsmaster/wmsdev/vendor/bcosca/fatfree/lib/base.php:2178] Base->error()
[F:/wmsmaster/wmsdev/tmp/2fhkm7wks1og8.2b7z992oxlwkk.php:7] Base->{closure}()
[F:/wmsmaster/wmsdev/vendor/bcosca/fatfree/lib/base.php:2708] require()
[F:/wmsmaster/wmsdev/vendor/bcosca/fatfree/lib/base.php:2925] View->sandbox()
[F:/wmsmaster/wmsdev/tmp/2fhkm7wks1og8.4dz5qziidi9i.php:3] Preview->render()
[F:/wmsmaster/wmsdev/vendor/bcosca/fatfree/lib/base.php:2708] require()
[F:/wmsmaster/wmsdev/vendor/bcosca/fatfree/lib/base.php:2925] View->sandbox()
[F:/wmsmaster/wmsdev/app/controllers/PageController.php:30] Preview->render()
[F:/wmsmaster/wmsdev/vendor/bcosca/fatfree/lib/base.php:1791] PageController->pageDisplay()
[F:/wmsmaster/wmsdev/vendor/bcosca/fatfree/lib/base.php:1612] Base->call()
[F:/wmsmaster/wmsdev/index.php:12] Base->run()
routes.ini
GET / = PageController->render
GET /@page = PageController->pageDisplay
this is the I get if my href link is /page/example-page

Not Found

HTTP 404 (GET /page/about_us)

[F:/wmsmaster/wmsdev/vendor/bcosca/fatfree/lib/base.php:1645] Base->error()
[F:/wmsmaster/wmsdev/index.php:12] Base->run()

If you can point me in the right direction I'd appreciate it! Thanks for listening!





xfra35

unread,
Sep 2, 2017, 8:47:21 AM9/2/17
to Fat-Free Framework
Illegal string offset 'page_name' indicates that in the expression @row.page_name, @row is a string (instead of an array).

The following line is probably the culprit:

Dwaine Halberg

unread,
Sep 3, 2017, 1:36:16 AM9/3/17
to Fat-Free Framework
Mmm that's weird if I can get the data displayed from the render function by hard coding id and just calling a full page template with the exact same var 'page_name' why does the data change from array to string? what makes including other template files change it?

ikkez

unread,
Sep 3, 2017, 4:01:23 AM9/3/17
to Fat-Free Framework
you mixed up the variables. I think you were about to write this:

$f3->set('row', $messages->getById($id)[0]);
$f3
->set('
wms_pages', 'main.htm');

Dwaine Halberg

unread,
Sep 3, 2017, 4:36:43 PM9/3/17
to Fat-Free Framework
that tells me the object of the pageModel could not be converted to a string...

Dwaine Halberg

unread,
Sep 4, 2017, 1:27:36 AM9/4/17
to f3-fra...@googlegroups.com
OK I found a working solution so I can continue the learning task, I had to change the way I called added the included to the template, by putting the includes in main template I can read pages. then I changed the function to call the page slug instead of the table id
page controller
    function pageDisplay($page)
   
{

        $pages
= new PageModel($this->db);

        $this
->f3->set('row', $pages->getBySlug($this->f3->get('PARAMS.page'))[0]);
        echo
\Template::instance()->render('main.htm');
   
}
main.htm
<include href="header.htm" />
<include href="menu.htm" />

       
<!-- Page Content -->
       
<div class="container">
   
           
<!-- Page Heading/Breadcrumbs -->
           
<div class="row">
               
<div class="col-lg-12">
                   
<h1 class="page-header">{{ @row.page_name }}</h1>
               
</div>
           
</div>
           
<!-- /.row -->
   
           
<!-- Content Row -->
           
<div class="row">
               
<div class="col-lg-12">
                    {{ @row.page_txt_1 | raw }} {{ @row.page_txt_2 | raw }}
               
</div>
           
</div>
           
<!-- /.row -->
<include href="footer.htm" />
 Maybe not the prettiest code but it's working for now until I can grasp the framework better.. thanks for those reply's
Reply all
Reply to author
Forward
0 new messages