CakePHP redirectng - cakephp

I have 2 view files, lets say file1.ctp and file2.ctp. What I want, when I am successfully done doing something in file1.ctp, it'll show me a "Successful" message and redirect me to file2.ctp. It should be automatic.

Controller::flash
From this comment, what you're describing is Controller::flash:
Controller::flash($message, $url, $pause, $layout)
Like redirect(), the flash() method is used to direct a user to a new page after an operation. The flash() method is different in that it shows a message before passing the user on to another URL.
Example usage
Consider 2 controller actions, like so:
function step1() {
...
$this->flash('Step1 complete, now starting step2', array('action' => 'step2'))
}
function step2() {
...
}
which when accessed at /example/step1 would execute the controller action, then show a plain page with the text Step1 complete, now starting step2, pause for 1 second (the default) and then send the user to /example/step2 using a meta refresh.

You can use the render function in CakePHP. Like This:
$this->render('file2');
You should use this in your Controller.

Related

CakePHP 3 - Passing parameters to application with a single controller where all requests are routed to it

CakePHP 3.x
In the Routing documentation (https://book.cakephp.org/3.0/en/development/routing.html) it says:
If you have a single controller in your application and you do not want the controller name to appear in the URL, you can map all URLs to actions in your controller. For example, to map all URLs to actions of the home controller, e.g have URLs like /demo instead of /home/demo, you can do the following:
$routes->connect('/:action', ['controller' => 'Home']);
That's fine as it means I can do stuff like this in my src/Controller/HomeController.php:
public function foo()
{
// Accessible through URL "/foo"
}
public function bar()
{
// Accessible through URL "/bar"
}
But if I need to pass parameters to a function, e.g.
public function baz($a, $b, $c)
{
}
It will give me a "Missing Controller" error if I call any of the following URLs:
/baz/2
/baz/2/17
/baz/2/17/99
It's saying that I need to create "BazController".
All of these, will work, because they include the controller name:
/home/baz/2
/home/baz/2/17
/home/baz/2/17/99
Presumably this is a routing problem?
Interestingly, calling /baz without any parameters works ok.
The correct answer, as provided in a comment by #arilia is:
$routes->connect('/:action/*', ['controller' => 'Home']);
The URL's provided in my question will now work as follows. All of these will be mapped to HomeController.php and execute the baz() function:
/baz/2
/baz/2/17
/baz/2/17/99
The * (at the end of /:action/* in app/routes.php) is key to allowing any number of parameters passed in the URL.

Kohana 3.3 request and response

One tutorial thought me, that I can fill my template with content, by putting:
$this->template->body = $this->response->body();
But, I cant see where the response is being populated. Even dumping the request and response, its just empty.
And secondly, how can I make advantage of the already built in Request class, to get some output from a function, without actually redirecting to that method ? Lets say:
$content = Request::factory('news/latest')->execute();
kind regards.
Using $this->template->body assumes that you are using Controller_Template controller. $this->template is a variable within Controller_Template representing View file application/views/template.php.
Setting something to $this->template->body passes $body variable to template view file. You can than use it (like echo $body;) within template view.
To get output from $content = Request::factory('news/latest')->execute(); your controller which handles news/latest Route must produce some output.
Than you can get this output like:
$response = Request::factory('news/latest')->execute();
$content = $response->body();

CakePHP reverse routing issues

I've been using routing with "slug" as a named parameter, for example:
Router::connect('/category/:slug', array('controller'=>'categories', 'action'=>'view'), array('pass'=>array('slug'), 'slug'=>'[a-z0-9\-]+'));
I've now stumbled across a problem because I want to restrict the above route to logged in users only, so I've put this in the beforeFilter() function of my CategoriesController:
if(!$this->Auth->loggedIn()) {
$this->Auth->deny('view');
}
Now if I go to /category/my-category (while logged out) I'll be redirected to my application's login page, unfortunately after I log in I'm redirected to /categories/view/my-category/slug:my-category
This is due to line 317 of AuthComponent.php, where we have:
$this->Session->write('Auth.redirect', Router::reverse($request));
So it seems when I do Router::reverse($request) on the above route it doesn't work properly (because it thinks "my-category" should be both a passed and a named parameter).
Is this a problem with the way I've set up this route, or is it a bug with CakePHP? Surely Router::reverse($request) should always return the URL we're currently at?
Any advice appreciated...
I'm not 100% sure if it is a bug or not, but until we find out a work-around could be to manually set the new loginRedirect in your category controller like so:
if(!$this->Auth->loggedIn()) {
$this->Auth->deny('view');
$this->Auth->loginRedirect = '/categories/' . $this->request->params['slug'];
}
Note, check that $this->request->params['slug'] is the right var to use, not 100% off the top of my head.

Cakephp - Having Issue when submitting form to plugin controller action

I am a 3+ years old in cakephp and facing a somewhat strange issue
with submitting a form to plugin controller's action (i am using
plugin first time). After trying different known things i am posting
this one.
Going straight into the matter here is the form in my "forum" plugin's search_controller.php's "index" view:
echo $form->create("Search", array('url'=>array('controller' =>
'search', 'action' => 'index','plugin'=>'forum'),
'id'=>'searchFormMain'));
<input type="text" name="data[Search][keyword]" style="width:357px; margin-left:9px;"><p><span id="searchButton"><input
type="image" src="/img/button_search.jpg" style="height:40px;width:
136px;border:0;" class="handcursor"></span></p>
</form>
As i am submitting this form to "index" action of search controller of
forum plugin, the following code does print nothing:
public function index($type='') {
if(!empty($this->data)) {
pr($this->data);
die;
}
}
While if i try the same code within beforeFilter of the same
controller i.e. search_controller.php it works well and prints as
follows:
Array
(
[Search] => Array
(
[keyword] => Hello Forum
)
)
And finally here is the beforeFilter code (of search_controller.php):
public function beforeFilter() {
parent::beforeFilter();
if(!empty($this->data)) {
pr($this->data);
}
}
Fyi, it does not matter if i comment out "parent::beforeFilter();" or
even disable $uses of my controller (if they look doubtful to you)
the result is same i.e. the control is not going in to "index" action
in the case of form submit while is working fine in the case of page
call. The url/action to page is http://localhost.rfdf.org/forum/search/index.
If i call the url directly it loads the form fine but when i submit it, it
never gets into the "index" action of the controller thus no view
rendered.
If i try the same set of code out of "forum" plugin environment i.e. in normal application it works just fine
I have been trying to find a way out of this for last 3+ hours now but
no success. I would appreciate any help in solving this puzzle.
I got it, finally!
It was Securty compontent dropping the request into the blackHole whenever it failed to find a security token with the form data. I learned that "Security" component "interferes" with $form->create() method and places a token as a hidden field with each $form->create() call. On the form submit, just after beforeFilter and right before getting into the controller "action" it checks for this token and simply dies everything on a validation failure. Unfortunately there is no error message or entry to cake log.
In my case i had been creating my own custom tag and not with the help of $form->create method so no token was being generated which lead to all the pain.
I resolved it by placing
$this->Security->validatePost = false;
at the end of beforeFilter.
Thanks everyone!
Have you tried putting an else into that if(!empty($this->data)) and doing a pr() as it could be that your post is not empty.
Either that or the format of your url array is not correct.
From ln759, http://api.cakephp.org/view_source/router/#line-757
$defaults = $params = array('plugin' => null, 'controller' => null, 'action' => 'index');
So I guess you need plugin first?
Are you using ACL or any of the like? In the beforeFilter, do a pr of the request. See which action is being requested to make sure that the request is correct

CakePhp - render a view + afterFilter

The problem is i want to call the index function, i need it to render the view and then the
afterFilter to redirect again to the index function and do the same.. Like a loop, the problem is it doesnt render it, ive tried using $this->render('index') but it doesnt work and also other things..
PS: I didnt include all the code that i have in index, because its pointless, doesnt render with or without it, just included the things i needed for the view.
function afterFilter()
{
if ($this->params['action'] == 'index')
{
sleep(3);
$this->redirect(array('action'=>'index',$id), null, true);
}
}
THE FUNCTION
function index($ido = 0)
{
$this->set('Operator', $this->Operator->read(null, $ido));
$this->set('ido', $ido);
}
THE VIEW = INDEX.CTP
<legend>Operator StandBy Window</legend>
<?php
?>
</fieldset>
<?php echo $html->link('LogIn', array('controller' => 'operators', 'action' => 'add')); ?>
<?php echo $html->link('LogOut', array('controller' => 'operators', 'action' => 'logout',$ido)); ?>
a function that constantly checks my database for a change, if a change occurs ill redirect, if not i need to have that constant loop of 'checking the database' and 'rendering the view'.
This is not possible entirely on the server with PHP, and especially not with CakePHP's template system. PHP just "makes pages" on the server and sends them to the browser and a linear fashion. If you loop on the server, the content of your page will just repeat itself:
Normal content
Normal content
Normal content
<redirect>
To redirect the client, you need to output headers. The headers need to be output before anything else. If you've already looped a few times and content has already been sent to the client, you can't redirect anymore.
There are two ways:
You just output one page at a time showing the current status, with a meta tag that'll refresh the page every x seconds. This can be rather expensive and annoying though.
Use AJAX and possibly Comet to update the information on the page dynamically in the browser.

Resources