I'm new to cakephp and trying to simply display form data once it is posted. I would like to type something on "add.ctp" which then redirects to "index.ctp" where the information I just typed should be displayed.
The reason why I'm doing this is because I like to echo my variables and forms at various places throughout my program for debugging purposes. I tend to work a lot with data that needs to be converted or manipulated so I like to check and make sure each part is doing its job correctly. I'm new to cakephp so I'm just trying to figure out how I can do this.
Here is the code for add.ctp where the information is entered.
View\Mysorts\add.ctp
<h1>Add Numbers</h1>
<?php
echo $this->Form->create('Mysort');
echo $this->Form->input('original');
echo $this->Form->end('Add Numbers');
?>
Here is my function in the controller
Controller\MysortsController.php
<?php
class MysortsController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
$this ->set('mysorts', $this->Mysort->find('all'));
}
public function add() {
if($this->request->is('post')) {
Configure::read();
pr($this->data); //attempting to print posted information
$this->redirect(array('action' => 'index'));
}
}
function isempty(){
$mysorts = $this->Mysort->find('all');
$this->set('mysorts', $mysorts);
}
}
?>
And finally, here is my index file where I would like to display the posted information.
View\Mysorts\index.ctp
<h1>Sorted Entries</h1>
<?php
echo $this->Html->link("Add List", array('controller'=>'mysorts', 'action' => 'add'));
if (!empty($mysorts)) {
?>
<table>
<tr>
<th>ID</th>
<th>Original</th>
<th>Sorted</th>
</tr>
<?php foreach ($mysorts as $mysort): ?>
<tr>
<td><?php echo $mysort['Mysort']['id']; ?></td>
<td>
<?php echo $mysort['Mysort']['original']; ?>
</td>
<td> <?php echo $mysort['Mysort']['sorted']; ?>
</td>
</tr>
<?php endforeach;
} else {
echo '<p>No results found!</p>';
}
?>
</table>
If the code you have posted is the exact you are using that could not work at all.
You do not save the data you receive while being in the "add" method. This is done by $this->ModelName->save($data) where ModelName is the Model to use (in your case it should be MySort and $data is the posted data.
You are using Cakephp2.x? I assume so cause you are using $this->request->is('post') which was not there in 1.3, i think. The problem about that is, that the posted data is not stored in $this->data anymore. It is in $this->request->data.
Do not use pr(). It is too "dangerous" to forget something like that in the code. Use debug() instead. The output will be disabled as soon as you see the DEBUG constant in Config/core.php in your application root to 0.
Calling the redirect() method in a controller generates a real 301 redirect. Which means the old output is dumped and lost. That and point 1 makes clear why you can not see anything. Nothing is saved and before you see the output of pr() your browser gets redirected. If you want to debug something use an exit; afterwards to make sure you wont miss the output. Sometimes you do not need it, but if you can not find your output, use it ;)
Hope this helps you.
Greetings
func0der
Maybe what you need is something like this.
For your add.ctp you define the action you want your post.
<h1>Add Numbers</h1>
<?php
echo $this->Form->create(array('action' => 'view'));
echo $this->Form->input('original');
echo $this->Form->end('Add Numbers');
?>
For your Controller You'll need to set the variable you want in your view
public function add() {
}
public function index(){
if($this->request->is('post')) {
$this->set('mysorts', $this->request->data);
}
}
And I'm not sure if what I see in your index.ctp makes sense.
I don't understand what your point is in trying to print something and then redirecting immediately? You won't see it when it redirects.
Anyhow, since your Form probably does not consist of a representation of an actual model, you might want to inspect your $this->params['form'] variable as opposed to the normal $this->data that you would use in a FormHelper.
Also, do you realize you are missing a closing } in your Controller\MysortsController.php? It closes the add() function but not the class...
Related
I'm having a rather odd problem with flash messenger in ZF2. I'm using it in quite a simple scenario, save a 'registration complete' message after registering and redirect to the login page and display the message however the messages are never returned by the flash messenger.
In controller register action:
$this->flashMessenger()->addMessage('Registration complete');
return $this->redirect()->toRoute('default', array('controller' => 'user', 'action' => 'login'));
In controller login action:
$flashMessenger = $this->flashMessenger();
$mes = $flashMessenger->hasMessages();
$cur = $flashMessenger->hasCurrentMessages();
Both $mes and $cur are false (I tried both just to be sure). Can anyone shed any light on this?
I'm using ZF 2.2.2 and PHP 5.3.14. Session save handler is using the dbtable adapter and I have tried disabling this as well as setting the flashmessenger session manager to the use the same dbtable save handler with no result.
To use the FlashMessenger controller plugin, you need to add the following in your controller:
<?php
class IndexController extends AbstractActionController {
public function indexAction() {
$this->flashMessenger()->addMessage('Your message');
return $this->redirect()->toRoute('admin/default', array('controller'=>'index', 'action'=>'thankyou'));
}
public function thankyouAction() {
return new ViewModel();
}
}
Add the following to the thankyou.phtml view template:
<?php
if ($this->flashMessenger()->hasMessages()) {
echo '<div class="alert alert-info">';
$messages = $this->flashMessenger()->getMessages();
foreach($messages as $message) {
echo $message;
}
echo '</div>';
}
?>
It seems that your code is as it should be, there must be something tricky in the workflow.
In this case, you can debug the old way : try var_dump($_SESSION) to see if it is populated by your flashMessenger.
Use
echo $this->flashMessenger()->renderCurrent(...
instead of
echo $this->flashMessenger()->render(...
I also faced same problem for (login flashmessage after registration) I solved it in the following way
Apply a check on your layout page like
<?php if($this->zfcUserIdentity()) { ?>
<div id="flashMessageDiv" class="hide">
<?php echo isset($flashMessages) && isset($flashMessages['0']) ? $flashMessages['0'] : ''; ?>
</div>
<?php } ?>
That means layout flashMessageDiv is accessible only to the logined user . now on your login view file (login.phtml)
apply the following code
<?php $pathArray = $_SERVER['HTTP_REFERER'];
$pathArray = explode("/",$pathArray);
?>
<?php if ($pathArray[4] === 'register') { ?>
<div id="flashMessageDiv" class="hide">
<?php echo "User details saved successfully"; ?>
</div>
<?php } ?>
In the above code i used HTTP_REFERER which will simply give us the referer url details check if referer url is register then show falshmessage.
Hope it will help you.
The FlashMessenger is now an official view helper in ZF2 and can be easily integrated in every view / layout:
FlashMessenger Helper — Zend Framework 2 2.3.1 documentation - Zend Framework
It works with TwitterBootstrap3 too and there is an alternative configuration for your module.config.php.
My index post controller list all posts in the following way
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'template'=>"{items}\n{pager}",
)); ?>
My view _view has the ajax-link
<div id="comments"></div>
<?php echo CHtml::ajaxLink('List Comments', array('listComments'),
array('update' => '#comments'))?>
listComments is a function in my PostController
public function actionListComments()
{
$this->renderPartial('_comments',array(
'post'=>$model,
'comments'=>$model->comments,
));
}
When I click to the ajax link , nothing happens,
it points to localhost/blog/#
Can you help me please ?
The problem is actionListComments() method returns non-200 HTTP code because of undefined $model variable in it. Try something like this:
_view:
<div id="comments"></div>
<?php echo CHtml::ajaxLink('List Comments', array('listComments', 'id' => $data->id),
array('update' => '#comments'))?>
PostController:
public function actionListComments($id)
{
$model = Posts::model()->findByPk($id);
if($model !== null)
$this->renderPartial('_comments',array(
'post'=>$model,
'comments'=>$model->comments,
));
else
Yii::log('Unknown post with $id ' . $id, 'error');
}
First in actionListComments() you have a variable $model which you haven't instantiated.
Assuming you are getting the $model->id from the link it should change to:
<?php echo CHtml::ajaxLink('List Comments', array('listComments','id'=>$data->id),
array('update' => '#comments'))?>
Next, your actionListComments() should access the id, use this to load a model and its comments, and send this to the required view
public function actionListComments($id){
$model=$this->loadModel($id);
$this->renderPartial('_comments',array('model'=>$model));
}
There is no need to send $model->comments as we are already sending $model therefore we can access $model->comments.
many things could go wrong about that. As ajax calls cant be debugged with normal compnonents like
CVarDumper::Dump();
die();
Above code will not show you anything in the browser area. The best way to debug ajax calls is using inspectElement. Click on Network. Now when you click on ajaxLink it will show you whether the ajax request was sent successfully. It will be red if the request was unsuccessful. When you click on the request made. It will show you 3 tabs on right named Header, Preview, Response. As you want to render the page so the content-Type should be text/html.
As far as your code is concerned clearly you are using $model without instantiating it so it is returning error.
Read the error returned in your case.
if my validation fails I do this:
return Redirect::back()->with('validation', $validation->errors->all());
also I am using:
$restful = true;
so when I am on get_edit() - I'am getting an error that there are no $validation variable when generating my view, when in post_edit() - its all okay because its returns a redirect with errors...
this is my view:
<? foreach($validation as $e): ?>
<div><?= $e; ?></div>
<? endforeach; ?>
undefined variable $validation, right now I'am trying to put it on the Router::before
Route::filter('before', function()
{
View::share('validation', array());
});
so the variable exists but is empty, but now arises a new problem, everytime after this filter executes it overrides those $validation that generates my post_edit(), also i've seen a variable $errors in my view but is ever empty, i don't how to use it, can you help me?
so shortly my problem is:
public function get_edit($id)
{
//generate my view with all nessesary data, but i can't generate here an error variable
// or its better to put it in one place to globally share it in the views, otherwise i am //getting an error
}
public function post_edit($id)
{
//validating $_POST data, if there is an error redirect it back to the get_edit() WITH a //variable containing errors
}
Did you read the docs? http://laravel.com/docs/5.0/validation#error-messages-and-views
You can use return Redirect::back()->withErrors($validation);
In your views, you can always use redirect('register')->withErrors($validator)$errors, without binding them to the view.
in AppController:
function beforeFilter() {
$company = 'name of Company';
$this->set(compact('company'));
}
in Controller class:
function companyinfo() {
$logo = '<div><?php $this->Html->image('logo'); ?></div>';
$welcome = 'welcome to $$company!';
$this->set(compact('logo','welcome'));
}
function beforeFilter() {
parent::beforeFilter();
}
in View class:
<html>
<body>
<?php echo $logo; ?>
<?php echo $welcome; ?>
</body>
</html>
it doesn't answer variable in view after passing the variable from AppController via controller..
1) When you use $this->set(compact('company'));, it is NOT setting a variable for use in any controller - it's passing $company to the view.
2) You're trying to write PHP code in a string, using a Helper (which are only available in Views)
$logo = '<div><?php $this->Html->image('logo'); ?></div>';
3) It's unusual to want to pass data from AppController to Controller to View.
What you probably want to do is something like this:
//App Controller
function beforeFilter() {
$company = 'name of Company';
$this->set(compact('company'));
}
//Controller
function companyinfo() {
$logo = 'logo';
$this->set(compact('logo'));
}
//Layout file (or view file, but I assume it's layout since you're getting data in the AppController)
<?php
echo '<div>' . $this->Html->image($logo) . '</div>';
echo "Welcome to " . $company;
I mean this in the most constructive way possible (we've all been there). It seems like you're struggling with some general PHP concepts. Before you get too heavy into CakePHP, I recommend trying out a few lengthy tutorials in generic PHP - then when you feel completely comfortable with it, dive into CakePHP.
To get company into the view you can set it in your appcontroller if you want it avaiable in ALL views throughout your site or inside a specific controller if you only want it available in the view of the function you set it inside of. Either way you'll need to make correct use of the set function. For example:
$this->set('company', 'Name of Company');
OR
$company = 'Name of Company';
$this->set('company', $company);
Afterwards you'll be able to access the $company variable in the view.
echo $company; outputs Name of Company
As for your question for Dave:
<?php $welcome = 'Welcome to $$company'; ?> <html><body><?php echo $welcome; ?></body></html>
would be written as:
<html><body><?php echo "Welcome to ". $company; ?></body></html>
However, you should really look into using layouts with cakephp so you don't need to the <html>, <head>, <body>, etc. tags in every view file
I have the following code:
<?php
if (!$this->Auth->user())
{
echo $this->element('header');
}
else
{
echo $this->element('header-bar');
}
?>
inside my view which should show a different header for logged in users but throws the following error:
Notice (8): Undefined property: View::$Auth [APP/views/layouts/page.ctp, line 17]
Fatal error: Call to a member function user() on a non-object in /Users/cameron/Sites/thehive/app/views/layouts/page.ctp on line 17
How do I fix this? Thanks
You don't need to do $this->set(compact('authUser'));
only use this in View:
if ($this->Session->read('Auth.User')){
// do something
}
As of CakePHP 2.x:
<?php if (AuthComponent::user('id')): ?>
Logged in as <?= AuthComponent::user('name') ?>
<?php endif; ?>
Note: Also check out meotimdihia's answer below. It's got a lot of upvotes.
The Auth component is for use in the Controller. You'll want to check for authorization in the controller, then set a variable for the view, e.g., $this->set('authUser', $this->Auth->user());. Then in your view you can do:
if (!$authUser)
{
echo $this->element('header');
}
If you want this to be done automatically for all controller methods, you can look into modifying cake/libs/controller/app_controller.php so that it includes the Auth component.
To summarize the answers on this page, evaluate one of the following based on which version of CakePHP you are using:
For version 1.x
$this->Session->read('Auth.User')
For version 2.x
AuthComponent::user('id')
For version 3.x
$this->request->session()->read('Auth.User.id')
For version 4.x, using the Authentication component
$this->loadHelper('Authentication.Identity');
...
$this->Identity->isLoggedIn()
This works in Cakephp 3+ (juts modify: "Auth.User.username" to suit your session data)
<?php
if (is_null($this->request->session()->read('Auth.User.username'))) {
echo "....logged out";
} else {
echo "You are Logged in As " . $this->request->session()->read('Auth.User.username');
}
?>
its been a while that I have used CakePHP but as far as I can remember CakePHP
doesn't support Auth in View. What you can do of course is set a variable in the
controller to use it in the view
<?
class AppController {
....
function beforeFilter(){
....
$this->set('auth',$this->Auth);
}
....
}
?>
and then use it in the view like this
$auth->....
or you can use the AuthHelper written by Ritesh Agrawal
http://bakery.cakephp.org/articles/ragrawal/2008/07/29/authhelper
BTW
I think if it comes to only test if somebody is logged in #webbiedave's answer
is better MVC style wise.
Nevertheless if you have to access userdata in view the just extract the userinfo
from Auth component and set it in the controller as I showed you and use it in the view
Regards
Try this
class AppController extends Controller{
$this->user = false;
public function beforeFilter(){
$this->user = $this->Auth->user();
}
public function beforeRender(){
$this->set('logged_user',$this->user);
}
}
Now You can check $logged_user in the view as
if($logged_user){
// users logged in $logged_user have all the details
}else{
// not logged in
}
in cakephp 3 you can check authentication session in the view like this
if($this->request->Session()->read('Auth.User')){
//do when login
}
else{
//do not login
}
If it helps anyone out in cakephp version 3.7.8 session has been depriciated to getSession so to update Lee Nielsen's comment
if (is_null($this->request->getSession()->read('Auth.User.username'))) {
echo "....logged out";
} else {
echo "You are Logged in As " . $this->request->getSession()->read('Auth.User.username');
}
For cakephp 4.2.6 Strawberry with Auth Component
<?php
// Note: Change email param to yours
if (is_null($this->request->getSession()->read('Auth')->email)) {
?>
<?= $this->Html->link(__('Login'), [ 'action' => 'login','controller' => 'Users']) ?>
<?php
} else { ?>
<?= $this->Html->link(__('Logout'), [ 'action' => 'logout','controller' => 'Users']) ?>
<?php }
?>
You need to set the user details from a controller, preferably the AppController which is inherited by all controllers across your site. Create/amend your app_controller.php to contain this beforeFilter(); method.
<?php
class AppController extends Controller {
function beforeFilter() {
$user = $this->Auth->user();
$this->set(compact('user'));
}
This will set a var called $user to the views which will be empty if the user is not logged in, or contain their data if they are.
//In the views (or layout)
$session->check('Auth.User.id');
//In controller
$this->Auth->User('id');
I found something worth mentioning, In CakePHP 3.x session handler is deprecated,
if we want to access session in view, we can do it via request handler. we have to use
<?php
// For CakePHP 3.x to access all user information
$this->request->session()->read('Auth.User');
// For CakePHP 3.x to check session
if($this->request->session()->read('Auth.User.id')) {
}
?>