Codeigniter 4 detect and differentiate homepage from other pages - codeigniter-4

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; ?>

Related

Display Barcode for each Item in view Codeigniter using zend barcode framework

I have to develop software for shop. i have list of product having their price and barcode digits. I want to show each and every items in product list html page along with barcode to take a print of barcode on stickers.
I googled about the zend barcode rendering and i tested also and found result ok with one barcode but here is my mai problem arise that i want to generate multiple barcode in foreach loop on my view->page.
I am very new to codeigniter please help.
My Controller 'Barcode.php'.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Barcode extends CI_Controller {
public function __construct(){
parent::__construct();
if($this->session->userdata('successful_logged_in')){
//load library
$this->load->library('zend');
//load in folder Zend
$this->zend->load('Zend/Barcode');
$this->load->model('common_model');
}
else{
redirect('login','refresh');
}
}
public function index(){
//Generate 13 digit random number to render barcode
$temp = rand(1111111111111,9999999999999);
$this->set_barcode($temp);
$table='item_master';
$order_by='name ASC';
$data['item_list']=$this->common_model->select_active_records($table,$order_by);
$this->load->view('home/header');
$this->load->view($this->router->fetch_class().'/records',$data);
$this->load->view('home/footer');
}
public function set_barcode($code){
//generate barcode
Zend_Barcode::render('code128', 'image', array('text'=>$code), array());
}
In my View 'records.php'.
<tbody>';
$i=1;
if($item_list==false){
echo '<tr class="text-danger"><td colspan="3"><strong<i class="fa fa-info-circle" style="font-size:12pt;"></i> - No record found</td></tr>';
}
else{
foreach ($item_list as $row){
echo '<tr>';
echo '<td>'.$i++.'</td>';
echo '<td>'.$row->name.'</td>';
echo '<td>'.$row->price.'</td>';
echo '<td>'.$row->barcode.'</td>';
echo '</tr>';
}
}
echo'
</tbody>
But this shows me only one barcode on entire html body page which was generated by random number
I Want the final result something like this
Expected Result
// Create a folder in your root directory like : root/barcode
public function set_barcode($code){
//generate barcode
$file = Zend_Barcode::render('code128', 'image', array('text'=>$code), array());
$code = time().'1222';
imagepng($file,"barcode/{$code}.png");
$data['barcode'] = $code.'.png';
$this->load->view('dashboard/products',$data);
}
// Add view page like this
<img src="<?php echo base_url().'barcode/'.$barcode; ?>" alt="">

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; ?>

What is the proper way to do conditional formatting in a ZF2 MVC project?

In a ZF2 project I am developing, I would like to create a shell around the echo $this->content; statement in layout.phtml that would allow conditional formatting of the main content area. Specifically, I want to put the content into a column that is 75% wide and include some “ornamental” elements in a column that is 25% wide for most of the pages. However, I want to change to a single column for pages that need more space. My project is a CMS in which each page has an attribute that can tell either the view or the controller whether the page should be normal or wide. I have considered a number of methods for achieving what I’m after.
My “conditional formatting in the layout view” might look like this:
// module/Application/view/layout/layout.phtml:
//...
if ($templateNormal) {
echo "<div class='col-md-9'>";
} else {
echo "<div class='col-md-12'>";
}
echo $this->content;
if ($templateNormal) {
echo "</div>";
echo "<div class='col-md-3'>";
//... ornamentation
echo "</div>";
} else {
echo "</div>";
}
//...
While the above-method could work, for pure MVC I don’t think there is supposed to be any decision-making going on in the layout view.
My “conditional formatting in partial views” could look like this:
// module/Application/view/layout/layout.phtml:
//...
echo $this->partial('partial/open-shell.phtml');
echo $this->content;
echo $this->partial('partial/close-shell.phtml');
//...
// module/Application/view/partial/open-shell.phtml:
if ($templateNormal) {
echo "<div class='col-md-9'>";
} else {
echo "<div class='col-md-12'>";
}
// module/Application/view/partial/close-shell.phtml:
if ($templateNormal) {
echo "</div>";
echo "<div class='col-md-3'>";
//... ornamentation
echo "</div>";
} else {
echo "</div>";
}
Here the decision-making is taken out of the layout view, but it is simply put into other views so it's still in the view package and still not pure MVC.
In my “conditional formatting in the controller” solution, a pair of html script strings are developed in a controller function, and then passed on to the view. It might look like this:
// module/Application/view/layout/layout.phtml:
//...
echo $this->open-shell-script';
echo $this->content;
echo $this->close-shell-script';
//...
// some controller function:
//...
if ($templateNormal) {
$open-shell-script = "<div class='col-md-9'>";
$close-shell-script = "</div>";
$close-shell-script = "<div class='col-md-3'>";
$close-shell-script .= //... ornamentation
$close-shell-script .= "</div>";
} else {
$open-shell-script = "<div class='col-md-12'>";
$close-shell-script = "</div>";
}
//...
In this method, the decision-making is done in the controller where I assume it should be, but it seems odd to be writing html there.
Any comments or suggestions?
create two layout and in init() method of Module.php decide which layout should use .
public function init(ModuleManager $moduleManager) {
$sharedEvents = $moduleManager->getEventManager()->getSharedManager();
$sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
// This event will only be fired when an ActionController under the MyModule namespace is dispatched.
$controller = $e->getTarget();
$controller->layout(" your chose layout ");
}
}
There are many ways to accomplish this. This is one method and the logic lives in the controller:
controller
public function yourSampleAction()
{
// assign variables as usual to this view model
$viewModel = new ViewModel(array(
//vars
);
// this will be the "wrapper" and can be single, double column or anything else.
$wrapperViewModel = new ViewModel();
$wrapperViewModel->addChild($viewModel, 'innerContent');
// use this line when you want one column
$wrapperViewModel->setTemplate('path/to/your/single-column-wrapper.phtml');
// when this line you want two columns
$wrapperViewModel->setTemplate('path/to/your/two-column-wrapper.phtml');
return $wrapperViewModel;
}
two-column-wrapper.phtml
<div class='col-md-9'>
<?php echo $innerConntent; ?>
</div>
<div class='col-md-3'>
<!--- something else in here? -->
</div>
single-column-wrapper.phtml
<div class='col-md-12'>
<?php echo $innerConntent; ?>
</div>
Instead of setting different templates, you can adjust the Bootstrap Twitter classes by making the necessary classes dependent on a layout variable. You can use the logic in your controllers action to pass variables directly to the layout (not the view) like so:
$this->layout()->setVariables
(
array
(
'layoutVar1' => 75,
'someColClass' => ($someSwitch ? 'col-md-9':'col-md-12' ),
'layoutVar1' => 75,
)
);
and then just access these variables in the Layout as you would variables sent to the View. You don't have to even prepend them with "layout", they won't clash.

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

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 ?>

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