I am trying to use session in controller between functions in Kohana 3:
public function action_setsession()
{
$session = Session::instance();
$session->set('test1', 'testing1');
$session->set('test2', 'testing2');
}
public function action_getsession()
{
$session = Session::instance();
$test1 = $session->get('test1');
$test2 = $session->get('test2');
echo 'Test1='.$test1;
echo 'Test2='.$test2;
}
And I do not get any data in getsession() function. Is it possible to use it in this way at all ? Or what I am doing wrong ? Thanks.
I can't see anything wrong with your code. It should work properly but there are some things to check:
Are you running action_setsession before you run action_getsession
Did you configure your session library properly? See Session Configuration
If you checked these things you most likely find the reason why it's not working.
Related
I've been trying to solve this strange problem. I just want to redirect a request to do a specific function using POST|GET method.
At first, I thought this was a straightforward scenario, but I found it little bit challenging.
I have this in my routing configuration (for GET method) :
[routes]
GET /pub/adsprocess/#command/#adsid=AdsController->processAds
and the code in my "view" file :
< a href="/pub/adsprocess/yes/1" >YES </a>
< a href="/pub/adsprocess/no/2" >NO </a>
My controller look like this :
class AdsController extends Controller
{
function processAds()
{
$command = $this->f3->get('PARAMS.command');
$ads_id = $this->f3->get('PARAMS.adsid');
/*some more process here...*/
}
}
As far as I see, everything looks ok to me. But, all of this doesn't work, F3 keep showing me "Method Not Allowed HTTP 405" error message. And this error message stay there when I use POST method, too.
I'm not sure where to look. Any clue or alternative approach to this problem will highly appreciated.
Thanks,
Sigit
as i can't comment, can we see the autoloader variable? I mean, if you place the AdsController in a Controller Folder and you didn't add the controller folder to the autoloader, then you need to access it like this:
\Controller\AdsController->processAds
and the AdsController.php file must be added the namespace to works:
Namespace Controller;
class AdsController extends \Controller
{
function processAds()
{
$command = $this->f3->get('PARAMS.command');
$ads_id = $this->f3->get('PARAMS.adsid');
/*some more process here...*/
}
}
if you did add it to the autoloader variable, it's should be work on your way.
In my SiteContoller I need to access the session on almost every action.
but I find write
$session = Yii::$app->session;
$session->open();
duplicate the same code on every action of the same controller is annoying.
anyway to solve this???
If you want to have your session opened in only SiteContoller you need to open it in before action method as so:
public function beforeAction($action) {
Yii::$app->session->open();
return parent::beforeAction($action);
}
I'm trying to implement a RESTful api in Laravel, and I for my index I want to return all the tasks as json.
However, when I use
return Response::json(Task::all());
I get an error: "The Response content must be a string or object implementing __toString(), "boolean" given".
I get the same error when I use:
return Task::all();
I thought this was supposed to work? What am I doing wrong?
I double-checked to see if Task::all() actually returns anything, and it does. This code does work in another project, although on another server and maybe another php version?
Someone suggested to use toArray(), but I get the same result. Code:
<?php
class UserController extends BaseController {
public function index() {
$users = User::all();
$userArray = $users->toArray();
return Response::json($userArray);
}
}
Response::json function is expecting argument 1 to be an array. From the API:
json( string|array $data = array(), integer $status = 200, array $headers = array() )
Return a new JSON response from the application.
So you can't just pass through the results of your find, but instead use the toArray() function and pass that through instead:
$tasks = Task::all();
Response::json($tasks->toArray());
Edit ---
If you're working with a BLOB, then base64_encode it first. See this post.
Example:
base64_encode($user['image']);
I'm using Zend Framework 1 with the Bisna library to integrate Doctrine 2. I generated my Entities from my database model with the Doctrine 2 CLI. This is all working fine, except for the setter methods for associated records. The argument they accept must be of a specific namespace (\Category here).
class Article
{
public function setCategory(\Category $category = null) {
$this->category = $category;
return $this;
}
}
However, when I do this:
$article = $this->em->getRepository('\Application\Entity\Article')->find(1);
$category = new \Application\Entity\Category();
$category->SetName('New Category');
$article->setCategory($category);
I get the following fatal error: Argument 1 passed to Application\Entity\CategoryField::setCategory() must be an instance of Category, instance of Application\Entity\Category given.
When I change the setter method to accept \Application\Entity\Category objects, it's working of course. Should I do this for every generated method, or are there other options? This is the first time I'm using namespaces, so it might be something simple.
You can always add this to the top of your class file: use \Application\Entity\Category; and then simply reference it later like so: public function setCategory(Category $category = null)
Check out: http://php.net/manual/en/language.namespaces.importing.php for more info about use
Otherwise you would have to reference the full namespace otherwise your application does not know that \Category is a reference to \Application\Entity\Category
I have a CodeIgniter application, but one of my controllers must call a data processing function that I have also written myself. The only problem is I can't seem to figure out how to do this. Looking through the user guide it seems that I should put my function inside the class declaration, and prefix it with an underscore (_) so that it cannot be called via the url. However, this is not working. Here's an example of what I mean:
<?php
class Listing extends Controller
{
function index()
{
$data = "hello";
$outputdata['string'] = _dprocess($data);
$this->load->view('view',$outputdata);
}
function _dprocess($d)
{
$output = "prefix - ".$d." - suffix";
return $output
}
}
?>
The page keeps telling me I have a call to an undefined function _dprocess()
How do I call my own functions?
Thanks!
Mala
Edit:
I've gotten it to work by placing the function outside of the class declaration. Is this the correct way of doing it?
This line is creating problem for you:
$outputdata['string'] = _dprocess($data);
Replace with:
$outputdata['string'] = $this->_dprocess($data);