How can I redirect page in codeigniter - redirect

I am beginning coding in codeigniter, here after the submit button the data should be insert on database and redirect to next page. For me inserting on database is successfully done but the page is not redirecting;
this is my controller - Home.php
but i am getting error as The requested URL /code/display was not found on this server.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('demo');
//$this->load->model('Insert_data');
}
public function savedata()
{
if($this->input->post('submit'))
{
$data=array(
'name'=>$this->input->post('name'),
'email'=>$this->input->post('email'),
'content'=>$this->input->post('content'));
$result=$this->Insert_data->saverecords($data);
if ($result)
{
echo "Records Saved Successfully";
redirect(base_url().'Home/display');
}
else
{
echo "Records not Saved Successfully";
}
}
}
public function display()
{
$this->load->view('display');
}
}

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('demo');
//$this->load->model('Insert_data');
}
public function savedata()
{
if($this->input->post('submit'))
{
$data=array(
'name'=>$this->input->post('name'),
'email'=>$this->input->post('email'),
'content'=>$this->input->post('content'));
$result=$this->Insert_data->saverecords($data);
if ($result==1)
{
echo "Records Saved Successfully";
redirect('Home/display');
}
else
{
echo "Records not Saved Successfully";
}
}
}
public function display()
{
$this->load->view('display');
}
}

Related

Own Laravel php artisan command in console.php

I have a file in app/Console/Commands/SendEmail.php with this content:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class SendEmail extends Command
{
protected $signature = 'send_email';
protected $description = 'Command description';
public function __construct()
{
parent::__construct();
}
public function handle()
{
echo "hello wordl\n";
}
}
I added these lines into routes/console.php:
use App\Console\Commands\SendEmail;
Artisan::command('send_email', function() { });
And the question is: what I need to write into the routes/console.php file to run this command?

zendframework action helper to display form

I want to have a login form when i click www.example.com/login with the help of action helper .
My controllers/helpers directory contain the file name
the class Login.php is
In my views/login/index.phtml i m calling it like this
<?= $this->loginForm; ?>
Login.php
<?
class Zend_Controller_Action_Helper_Login extends
Zend_Controller_Action_Helper_Abstract
{
function Login($a)
{
//Consists of form elements
}
}
?>
I get the following error ---------
"Helper "login" does not support overloading via direct()"
How to display the form and what is the error about ?
I was able to get it right this way .
http://geekabyte.blogspot.nl/2012/08/understanding-zend-frameworks-plugin.html
in the Bootstrap.php
public function _initLoader()
{
//For configuring Zend Application to be aware of the Controller Action Helper
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH .'/controllers/helpers');
}
The LoginController.php
class LoginController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$this->_helper->login->scream();
}
}
In the controller/helpers/Login.php
class Zend_Controller_Action_Helper_Login extends Zend_Controller_Action_Helper_Abstract
{
Public function scream()
{
echo "Here please help!!!";
}
}

Zend + Doctrine + PHPUnit = There is no open connection

I'm trying to create unit test with phpunit for Zend, using Doctrine ORM. When I try to create a test class extending Zend_Test_PHPUnit_DatabaseTestCase, I get a message when executing PHPUnit: "There is no open connection"
Here is the full source:
<?php
class AclTest extends Zend_Test_PHPUnit_DatabaseTestCase
{
private $_userAdmin;
public function setUp()
{
$this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
$this->_userAdmin = Model_UserTable::getInstance()->findOneByUsername('admin');
parent::setUp();
}
protected function getConnection()
{
$pdo = new PDO('mysql:host=localhost;dbname=mydbname', 'root', 'pwd');
return $this->createDefaultDBConnection($pdo, 'testdb');
}
protected function getDataSet()
{
return null;
}
public function testHasProfilPermission()
{
//execute some tests
}
}
What do you think ?
Thanks
Try this, please:
class AclTest extends Zend_Test_PHPUnit_DatabaseTestCase
{
private $_userAdmin;
/** #var PDO **/
protected $pdo;
public function __construct()
{
$this->pdo = new PDO('mysql:host=localhost;dbname=mydbname', 'root', 'pwd');
}
public function setUp()
{
$this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
$this->_userAdmin = Model_UserTable::getInstance()->findOneByUsername('admin');
parent::setUp();
}
protected function getConnection()
{
return $this->createDefaultDBConnection($this->pdo, 'testdb');
}
}

Zend Framework: How to render a different action?

I have two actions that are essentially identical, but need different URLs. Normally I would use _forward() to render the other action:
class MyController extends Zend_Controller_Action
{
public function actionOneAction()
{
$this->_forward('action-two');
}
public function actionTwoAction()
{
$this->view->foobar = 'foobar';
}
}
However, I have some code the is happening in preDispatch() that I only want to execute once:
class MyController extends Zend_Controller_Action
{
public function preDispatch()
{
//execute this only once before actionOne or actionTwo, but not both
}
public function actionOneAction()
{
$this->_forward('action-two'); //this won't work because preDispatch() will get called a second time
}
public function actionTwoAction()
{
$this->view->foobar = 'foobar';
}
}
So I thought maybe I could simply call the function directly, like this:
class MyController extends Zend_Controller_Action
{
public function preDispatch()
{
//execute this only once before actionOne or actionTwo, but not both
}
public function actionOneAction()
{
$this->actionTwoAction(); //execute the same code as actionTwoAction()
}
public function actionTwoAction()
{
$this->view->foobar = 'foobar';
}
}
But now Zend Framework is complaining about not being able to find the action-one.phtml view script. I don't want to render actionOne's view script. I want to render actionTwo's view script. What do I need to do?
Using render() seems to do the trick:
public function actionOneAction()
{
$this->actionTwoAction(); //execute the same code as actionTwoAction()
$this->render('action-two'); //renders the same view as actionTwoAction()
}

Referencing variable set by application in models (a good idea?)

i am using zend framework 1.10 with doctrine 2. i wonder if in my (doctrine) model class, isit a good idea to reference a variable set by my application (bootstrap.php, variable stored in Zend_Registry, i think its something like a global variable)
what i want to access is the doctrine entityManager. also i want the id of the logged in user
I am building a project with similar setup (ZF 1.10 + Doctrine2) and I've used dependency injection to deal with this situation, much like takeshin said. Here goes full project repository URL: https://bitbucket.org/phpfour/zf-doctrine2. Below are some code excerpts.
Here's my controller:
<?php
require_once APPLICATION_PATH . "/models/PostManager.php";
class IndexController extends Zend_Controller_Action
{
private $_em;
public function init()
{
$this->_em = $this->getInvokeArg('bootstrap')->getResource('doctrine');
}
public function indexAction()
{
$pm = new PostManager($this->_em);
$this->view->posts = $pm->getPublicPosts();
}
My entity manager (or service class):
<?php
class PostManager
{
protected $_em;
public function __construct(Doctrine\ORM\EntityManager $em)
{
$this->_em = $em;
}
public function getPublicPosts()
{
$query = $this->_em->createQuery('SELECT p FROM Entities\Post p WHERE p.visible = true');
$posts = $query->getResult();
return $posts;
}
Hope this helps!
you should simply use Zend_Auth for the logged-in-userId problem, then could do something like the following in your model
class Model extends BaseModel
{
public function something()
{
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
$loggedInUserId = $auth->getIdentity()->id;
}
}
}
There is nothing wrong with this approach (unless you are referring to singletons). Use dependency injection where possible.
However I'd create a service (or two) for this.
class Modulename_Services_Servicename
{
public function getCurrentUser() { ... }
public function getCurrentUserModel() { ... }
public function isLogged() { ... }
public function authenticate() { ... }
public function getSomeData()
{
$user = $this->getCurrentUser()
$model = new YourModel($user);
$query = ....;
$result = $query->execute();
return $result;
}
public function getSomeMoreData($usermodel) { ... }
}