fabulog icon indicating copy to clipboard operation
fabulog copied to clipboard

Trying to redirect to another controller...

Open mpfrenette opened this issue 7 years ago • 2 comments

In order to learn Fat-Free-Framework, I decided to start tweaking Fabulog to add support for Pages (I can share my code when I am done)

Here is what I did:

1 ) Created a new model "Page", new Admin views "page-list.html", "page_edit.html", "page_layout.html", and a new controller "Page". Obviously, these are tweaked copies of your Post versions.

2 ) I added the Page section in the admin menu

3 ) I changed the routing at follows:

             // view list
           $f3->route(array(
              'GET /',
               'GET /page/@page'
              ),'Controller\Post->getList');

        // view single

        // pages have priority: if the slug isn't found in page, we will call Post->getsingle
         $f3->route(array(
             'GET /@slug',
            'GET /p/@slug',
           ), 'Controller\Page->getSingle');


$f3->route(array(
    'GET /post/@id'
   ), 'Controller\Post->getSingle');

4 ) In my page not found handling, I added this:

			if ($this->resource->dry()){
				if (!isset($params['id'])) {

					$postcontroller= new Post();
					$postcontroller->getSingle($f3, $params);

/*
					$f3->route(array(
    			'GET /'. $params['slug'],
       ), 'Controller\Page->getSingle');
*/
				}
				else{
					$f3->error(404, 'Page not found');
				}
			}

But neither methods work...

I instead get a page without the post loaded.

I can simply redirect to /post/@slug

But I was hoping to avoid doing that...

Do you have any ideas?

Here are my thoughts:

1 ) I do not yet fully understand the resource, so it's quite possible that the resource is still trying to work with pages.

2 ) Perhaps it's not possible to route or call a new controller from within a controller without extra initialization?

mpfrenette avatar Mar 08 '17 12:03 mpfrenette

I can't seem to make the markup work....

Here is the source code of my controller page.txt

mpfrenette avatar Mar 08 '17 13:03 mpfrenette

The problem may be that you are getting the post but not actually assigning any of the post data to anything that will be rendered in the view.

Take a look at these lines `

                // copy whole page model, to be able to fetch relations
		// on the fly from within the template, if we need them

		$this->response->data['page']=$this->resource;
		$f3->set('page.title',$this->resource->title.' - '.\Config::instance()->blog_title);`

$f3->set is setting the data that will be rendered in the view.

Once you get the post you need to actually set something in the response that will be read on the front end.

Here's some doc on $f3->set https://fatfreeframework.com/3.6/base

If you want to actually call the posts view as well, you're better off redirecting.

codypatnaude avatar Mar 14 '17 16:03 codypatnaude