Kohana ErrorException [ Fatal Error ]: Call to undefined method Request::redirect() - kohana-3

I'm using Kohana 3.3.0 and i have a controller which is supposed to save blog articles to a database then redirect to the homepage, my code is as follows:-
class Controller_Article extends Controller {
const INDEX_PAGE = 'index.php/article';
public function action_post() {
$article_id = $this->request->param('id');
$article = new Model_Article($article_id);
$article->values($_POST); // populate $article object from $_POST array
$article->save(); // saves article to database
$this->request->redirect(self::INDEX_PAGE);
}
The article saves to database but the redirect line gives the error:-
ErrorException [ Fatal Error ]: Call to undefined method Request::redirect()
Please let me know how i can do the redirect.
Thanks

You're getting the Exception because as of Kohana 3.3, Request no longer has the method redirect.
You can fix your example by replacing
$this->request->redirect(self::INDEX_PAGE);
with
HTTP::redirect(self::INDEX_PAGE);

Yeah, Request::redirect is not longer exists. So in order to easily to move from 3.2 to 3.3 I extented Kohana_Request class and added redirect method. Just create Request.php in classes folder and write
class Request extends Kohana_Request {
/**
* Kohana Redirect Method
* #param string $url
*/
public function redirect($url) {
HTTP::redirect($url);
}
}
So you will be able to use both Request::redirect and $this->request->redirect

in your controller $this->redirect('page');

$this->redirect('article/index');

Related

Error in setting up Twig with CodeIgniter

I am trying to set up Twig Templating Engine with my new Codeigniter project.
But I am getting this error after setting up
Twig Exception
There are no registered paths for namespace "__main__".
I don't know what this error means. and how to solve it.
As per I am checking this is originated from the following file-
/application/vendor/twig/twig/lib/Twig/Loader/Filesystem.php:
code:
class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderInterface, Twig_SourceContextLoaderInterface
{
/** Identifier of the main namespace. */
const MAIN_NAMESPACE = '__main__';
protected $paths = array();
protected $cache = array();
protected $errorCache = array();
private $rootPath;
...
I followed this article for setting up
https://github.com/bcit-ci/CodeIgniter/wiki/Twig-PHP-Template-Engine-Implementation
Codeigniter Version- 3.1.8
Finally I made it.
I was missing this entry in autoload.php file
$autoload['config'] = array('twig');

Load Controller With Parameter Opencart

I have created one controller inside catalog/controller/module/same_collection.php
Inside that :
class ControllerModuleSameCollection extends Controller {
//User Product History
public function index($product_id) {
echo $product_id;
}
}
I have try to call it inside another controller like this
$data['same_color'] = $this->load->controller('module/same_color' ,['product_id' => 2] );
and I try to by access it using url like this
mydomain.com/index.php?route=module/same_collection&product_id=2
but it's not working.
Please help!!!
It seems like you have not created your module properly. Make sure your module's setting is saved in module table . If your module's code is not there in the table then you won't get any parameter in your index() method.

Black-hole error when route pointed to a class that extends plugin and uses extended class

In routes I have
Router::connect('/opauth-complete/*', array('controller' => 'app_users', 'action' => 'opauth_complete'));
If I change pointer to controller app_users with anything else and create controller everything works with no error. But I need it to work with AppUsersController.
AppUsersController looks like this
App::uses('UsersController', 'Users.Controller');
class AppUsersController extends UsersController {
public function beforeFilter() {
parent::beforeFilter();
$this->User = ClassRegistry::init('AppUser');
}
// ...
// ...
public function opauth_complete() {
die(1);
}
// ...
// ...
}
So, plugin is CakeDC Users and another plugin that goes to /example/callback after /example/auth/facebook is Opauth plugin.
Error message looks like this
The request has been black-holed
Error: The requested address '/example/opauth-complete' was not found on this server.
This is perfectly possible to make these two plugins work together; when browser points to /example/auth/facebook, it redirects to /example/auth/callback and somehow it needs opauth-complete route to link to specific method.
All works if not pointed to app_users that extends plugin, uses plugin. Does not work only with this case. How can users of these two plugins get around such situation.
I solved it by disabling Security component on Opauth action in my AppUsersController. Thing is that Opauth transfers data using POST and you should either change a method of it (ie: use Sessions, GET) or disable Security component.
For a method change use this in your bootstrap.php or core.php
Configure::write('Opauth.callback_transport', 'session'); // you can try 'get' too
To follow my approach add this to a controller where error occurs and where you place your opauth_complete method
public function beforeFilter() {
// ...
if (isset($this->Security) && $this->action == 'opauth_complete') {
$this->Security->validatePost = false;
$this->Security->csrfCheck = false;
}
// ...
}
P.S. Changing method to Sessions has its drawbacks, you can take a look at comments here at Github Opauth issue #16

Kohana 3.3 multi language

Following the tutorial # http://kerkness.ca/kowiki/doku.php?id=example_of_a_multi-language_website
I need to override the Request class, but there is no instance method.
What do i need to change to get it working in kohana v3.3 ?
Thx in advance!
The use of Request::instance() was replaced with Request::$current since Kohana 3.2, so you should write this in top of your Request class:
class Request extends Kohana_Request {
public static function current( & $uri = TRUE)
{
...

Why can't Kohana find my controller?

I have the following controller:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Static extends Controller_DefaultTemplate {
public function action_index()
{
View::set_global('message', '<span class="highlight">This is a global message.</span>');
$data = array (
'siteTitle' => 'Kohana Test Site',
'siteSubtitle' => 'A site to learn Kohana',
'menu' => View::factory('blocks/menu'),
);
$view = View::factory('templates/layout', $data);
$this->request->response = $view->render();
}
}
but kohana gives me the error:
ErrorException [ Fatal Error ]: Class
'Controller_DefaultTemplate' not found
although Eclipse can find the file (via F3) and I thought Kohana was able to find all classes via autoloading?
How can I get Kohana to find the Controller_DefaultTemplate class so I can extend Controller_Static?
You must include file with definition of Controller_DefaultTemplate
The problem was that my file name defaultTemplate.php was camel case, changing it to all-lowercase defaultemplate.php enabled Kohana to find the class inside it.

Resources