In zf2 how to load more than one action in controller in a page - layout

How to show both of my Signin and Signup module in a same page,both work fine seperately in their route
www.mydomain.com/Signin
www.mydomain.com/SignUp
How it is possible to include/call signup action while in Signin Action ?
Is there any website in LIVE that had been done in ZF2 ?
Can i get any sample project of Zf2 except ZendSkeltonApplication (It doesn't have two actions in a same page) ?

Try use like this, action in your controller
public function loginAction()
{
$signin = $this->forward()
->dispatch('App\Controller\Signin');
$signup = $this->forward()
->dispatch('App\Controller\Signup');
$page = new ViewModel();
$page->addChild($signin, 'signinBlock');
$page->addChild($signup, 'signupBlock');
return $page;
}
in your view
<?php echo $this->signinBlock ?>
<?php echo $this->signupBlock ?>

Related

Codeigniter 4 detect and differentiate homepage from other pages

I would like to differentiate my homepage with other pages as my structure is a little different from other pages but I have no clue is it to make use of public function view(). Currently I am working on 1 page which is my profile page, the link is localhost/account/profile, in this case, how do I differentiate homepage and account page? Below are my codes :
HTML
<header>
<nav></nav>
// only show slider if it's homepage
<slider></slider>
</header>
Controller
class Account extends BaseController
{
public function index()
{
echo view('templates/header');
echo view('account/profile');
echo view('templates/footer');
}
public function login()
{
echo view('templates/header');
echo view('account/login');
echo view('templates/footer');
}
public function register()
{
echo view('templates/header');
echo view('account/register');
echo view('templates/footer');
}
//--------------------------------------------------------------------
}
Routes
$routes->match(['get', 'post'], 'account/register', 'Account::register');
$routes->match(['get', 'post'], 'account/login', 'Account::login');
$routes->match(['get', 'post'], 'account/profile', 'Account::index');
Please let me know if the information I needed is insufficient. Thanks in advance guys.
Hmm... Somehow it's working using this method for me. Not sure whether this is a good way though.
<?php
$uri = service('uri');
if($uri->getSegment(1) == ''):
?>
<slider></slider>
<?php endif; ?>

Wordpress is showing paginated pages that doesn't exist

I have a page template where I run a WP_Query
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('post_type=post&posts_per_page=4'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
The issue I'm facing is that if you go to the site /page/100 it will show the template and it's not going to a 404 when It should.
My blog page under reading settings is another page and this is a custom template I'm doing.
I have read this post https://wordpress.stackexchange.com/questions/46116/non-existing-blog-pages-are-not-redirected-to-404 and tried all the functions and none of them work.
I also spent 3 hours searching on google without being able to find a workaround.
You are creating a new WP Query. So I think you have to reset the wp_postdata.
WP Codex offers sample code. You could try something like
$wp_query = new WP_Query();
$wp_query->query('post_type=post&posts_per_page=4'.'&paged='.$paged);
if( ($wp_query->have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post();
// Do here something with the post
endwhile;
wp_reset_postdata(); // At the end reset your query
endif;
While using WP_Query for displaying posts list you need to check by yourself if they are any posts for current page. Below is example code from Wordpress docs
<?php if ( have_posts() ) : ?>
<!-- Add the pagination functions here. -->
<!-- Start of the main loop. -->
<?php while ( have_posts() ) : the_post(); ?>
<!-- the rest of your theme's main loop -->
<?php endwhile; ?>
<!-- End of the main loop -->
<!-- Add the pagination functions here. -->
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Replace " _e('Sorry, no posts matched your criteria.'); "
with own error or function displaying 404 error.
Edit: Above code is of course example for Main Loop. In your particular case you need to call $wp_query->have_posts() and make sure it not evaluates to false. If it does display 404 error.
for example:
if(!$wp_query->have_posts()) {
echo "404";
}
Or if you want server respond with HTTP code 404 instead of 200 then you should try something like this:
if(!$wp_query->have_posts()) {
status_header(404);
nocache_headers();
include( get_404_template() );
exit;
}

disable layout for particular pages in ZF2

How to disable particular layout(example:menus.phtml) for particular pages in controller in ZF2?? In the below example menus.phtml should be disable for specific pages. Remaining pages must contain menus.phtml like header and footer.
<div>
header.phtml
</div>
<div>
menus.phtml
</div>
<div>
<?php echo $this->content; ?>
</div>
<div>
footer.phtml
</div>
There are various aproaches to this. Also modules.zendframework has quite a few modules here that may help you out.
If you are still keen on writing that yourself you could add variables to your layout within your controllers like so:
<?php
//YourController.php
public function someAction()
{
...
$this->layout()->footer = 'default';
...
}
//layout.phtml
<?php if ($this->footer === 'default') : ?>
//show the footer
<?php endif; ?>
Doing this is pretty inefficient though. Just imagine you'd need to do this to every action in all the controllers... I sure would not like to do that.
Now zf2 has a service and event layer that could help us out quite a bit here. This is a pretty nice read and introduction to it. You'd just write a service and trigger a event on your controllers/routes/whatever. Now you would also probably like to configure what is shown and what is hidden right? Thats pretty easy, too. Just write yourself a config file and merge it with the global.config like so:
<?php
//CustomModule/module.php
public function getConfig() {
$config = array();
$configFiles = array(
include __DIR__ . '/config/module.config.php',
include __DIR__ . '/config/module.customconfig.php',
);
foreach ($configFiles as $file) {
$config = \Zend\Stdlib\ArrayUtils::merge($config, $file);
}
return $config;
}
Source: Where to put custom settings in Zend Framework 2?
First, get the controller or action name:
$controllerName =$this->params('controller');
$actionName = $this->params('action');
then in your layout/view script add a simple logic.
<?php if ($actionName != 'action that you want to disable the layout/menu'): ?>
echo $this->render('menus.phtml');
<?php endif; ?>

(Post/Redirect/Get pattern) Laravel layout variables don't work after redirect

I'm using the Post/Redirect/Get (PRG) pattern in my Laravel controllers to prevent duplicate form submission.
It works well when I don't use layouts or when my layouts don't use any variable. The problem is my layout uses a variable named $title. When I load the view and the layout without redirect it works well, the title set in the controller is passed to the layout, but after processing a form and redirecting to the same route which uses the same layout and the same controller method I get a "Undefined variable: title" error coming from my layout file.
Here is my code:
File: app/routes.php
Route::get('contact', array('as' => 'show.contact.form', 'uses' => 'HomeController#showContactForm'));
Route::post('contact', array('as' => 'send.contact.email', 'uses' => 'HomeController#sendContactEmail'));
File: app/controllers/HomeController.php
class HomeController extends BaseController {
protected $layout = 'layouts.master';
public function showContactForm()
{
$this->layout->title = 'Contact form';
$this->layout->content = View::make('contact-form');
}
public function sendContactEmail()
{
$rules = ['email' => 'required|email', 'message' => 'required'];
$input = Input::only(array_keys($rules));
$validator = Validator::make($input, $rules);
if($validator->fails())
return Redirect::back()->withInput($input)->withErrors($validator);
// Code to send email omitted as is not relevant
Redirect::back()->withSuccess('Message sent!');
}
}
File: app/views/layouts/master.blade.php
<!DOCTYPE html>
<html>
<head>
<title>{{{ $title }}}</title>
</head>
<body>
#yield('body')
</body>
</html>
File: app/views/contact-form.blade.php
#section('body')
#if (Session::has('success'))
<div class="success">{{ Session::get('success') }}</div>
#endif
{{
Form::open(['route' => 'send.contact.email']),
Form::email('email', null, ['placeholder' => 'E-mail']),
Form::textarea('message', null, ['placeholder' => 'Message']),
Form::submit(_('Send')),
Form::close()
}}
#stop
I don't understand why after redirecting the next line of code is ignored
$this->layout->title = 'Contact form';
I've tried with Redirect::action('HomeController#sendContactEmail'); or Redirect::route('show.contact.form'); but the result is the same.
The controller in charge of rendering that view is exactly the same before the redirect than after the redirect, and it has no business logic at all, so why it only works on the first case but not in the second?
This
Redirect::back()->withSuccess('Message sent!');
should be
return Redirect::back()->withSuccess('Message sent!');
When layout attribute is set in a controller and method is not returning any response, controller try to render the layout. In your sendContactEmail() method both conditions fulfilled and controller tried to render layout before $title is set.
see callAction() in Illuminate\Routing\Controllers\controller.
http://laravel.com/api/source-class-Illuminate.Routing.Controllers.Controller.html#93-127
Have you tried using
return View::make('contact-form', array('title' => 'Contact Form'));
Instead of interacting with the layout directly?
Redirect::back() creates a 302 using the referer value of the current HTTP request. I would start by comparing the initial form request to the redirect request to see if that yields any clues. You could also try...
Redirect::route('HomeController#showContactForm')->withInput()...
I know it's less dynamic but it will generate the URL rather then rely on the referer value in the HTTP header.

How to get the request parameters that are passed from Request made by view in Kohana 3.3.0

I am trying to echo a request in the view file in Kohana using Request::factory() method and i am sending a value in that request which i am unable to get in the User Controller here is my code:
The View file:
<h1> Welcome to My First View File </h1>
<?php echo Request::factory("user",array("id" => 123))->execute(); ?>
Then the User.php Controller have this code:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_User extends Controller {
public function action_index()
{
$value = $this->request->param('id');
$content = View::factory('menu')->bind("id", $value);
$this->response->body($content);
}
} // End User
and the view menu.php have this code:
<h2> This is the view called by Request and Parameters send was:
<?php echo $id; ?>
</h2>
when i run the code it display the text This is the view called by Request and Parameters send was: but it doesn't display the $id anyone can tell me why?
P.S: sorry for my bad English as its not my native language
Here you can see, that Request::factory() requires URI value a the first param. So, you should call something like:
<h1> Welcome to My First View File </h1>
<?php echo Request::factory(Route::get("user")->uri(array("id" => 123)))->execute(); ?>
or
<h1> Welcome to My First View File </h1>
<?php echo Request::factory("user/123")->execute(); ?>
First example uses reverse routing, where "user" is a Route name. I assume that you already have Route for handling URIs like '/user/123'.

Resources