I have a basic application being baked by CakePHP.
Now i want to add a new action'listjobs' in my model controller class and a corresponding 'listjobs' view in the /Template/Job/listjobs.ctp.
From my model's index view i added one more action in the side navigation bar.
like this
<li><?= $this->Html->link(__('View Jobs'),['action' => 'listjobs']) ?></li>
When i click on the link 'View Jobs' control is directed to the action method of my model's controller but its not taking the list jobs 's view.
Code for my action method
public function listjobs()
{
$this->log("inside list jobs",'debug');
$this->render('listjobs');
}
The listjobs.ctp contains a very basic code as following
<div class="actions columns large-2 medium-3">
<h3><?= __('Actions') ?></h3>
<ul class="side-nav">
<li>< Git Hub >
</li>
<li>< Stack overflow >
</li>
</ul>
</div>
Not able to find out why the view is not rendered rather the index view is getting rendered.
Links loaded according to chrome console in order
(1) http://localhost/myjobs/job/listjobs
(2) http://localhost/myjobs/job
So (1) should have been loaded with the view..it should not been redirected to (2)
Regards,
Saurav
I think you need to use like this.
<li><?php echo $this->Html->link('View Jobs',array('controller' => 'MyModel', 'action' => 'listjobs')); ?></li>
Your view will be.
public function listjobs()
{
$this->log("inside list jobs",'debug');
}
I think you don't need to render, your view will be render by default, you will need to send data like that $this->set(compact('VarName'));
Secondly $this->log("inside list jobs",'debug'); if log is a method then you need to set action, then it will be.
$this->setAction('log');
Try, if you get error, then let me know. If your work proper then, you view will work.
http://localhost/myjobs/job/listjobs
this link defines.
http://localhost/SiteName/ControllerName/ActionName/Parameter
Get review and study this page.
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html
Thanks
ok...i did some debugging of the cakephp framework code.
I was using an auth component for my login functionality.
I found that the newly added action was not allowed to pass the through filter of auth component. so i added the newly added action in the beforefilter method of appcontroller like this. i guess the crud operations are already added in the filter by default.
$this->Auth->allow(['index', 'view', 'display','listjobs','add','delete']);
cheers,
Saurav
Related
I'm new to Laravel 5.4 and php and I'm having some trouble to do my routing, so I wish you would explain to me what's going on !
I'm developing a back office for my future projects, and for my current project I need to be able to update data sent to the homepage. The edit of the page and the update of the data is now fully functional, I'm just having some trouble to do the link in the layout. Pretty sure it's a trivial problem, yet i'm having trouble figuring it out.
Here is my code :
web.php :
Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function()
{
Route::resource('homepage', 'Admin\HomeController');
});
HomeController :`
public function edit($id) {
$contentExists = Homepage::first();
$homepage = Homepage::findOrFail($id);
return view('admin/homepage/edit', compact('homepage', 'contentExists'));
}`
In the layout :
<ul class="list-unstyled">
<li>
Page d'accueil
</li>
</ul>
Now for that I know I need to pass a parameter to the route, so I did like so :
<ul class="list-unstyled">
<li>
Page d'accueil
</li>
</ul>
But then it says the homepage variable is not defined, and I have no idea where I can define this variable since I'm in the layout, and also have no idea what needs to be inside this variable, what's his purpose... Can you help me with that ?
route second parameter, must be an array like this for example:
route('homepage.edit', ['id' => 2])
I am doing a small project using Yii2.
Suppose I have same layout (header, footer) in a view (eg site) except a login.php in this view. I want a different or no header / footer in this file. What can I do the remove the header / footer only from this view file.
All I could get to change layout in different views. Is it possible to change layout in a single file of a view?
Inside the relative action:
public function actionYourAction($id)
{
$this->layout = 'yourNewLayout';
return $this->render('yourView', [
'model' =>$model,
]);
}
I am a little late to the party, but you CAN change your layout from within your view. You do not have to declare it in your controller. I personally think it is better to do it in the view, because you can easily see later what is going on. If your making HTML edits, you would go into the view file, and easily be able to see which layout it is using. Putting this in the Controller, you (or someone later on) might miss the layout change nested into your controller's action.
Since $this refers to your view in Yii2 and not your controller as it did in Yii1, the old $this->layout doesn't work anymore from within your view.
Now, in Yii2, you refer to the controller from your view using $this->context.
$this->context->layout = 'your-layout';
In my project I wanted 2 layouts: one for site and one for the webapp. As the main.php file is the default layout, I've created a site.php layout and in the beginning of the siteController, just after the class declaration, I've put
public $layout = 'site';
The result is that only the siteController rendered views are using the site.php layout. It worked for me.
I'm also a litte late to the party, but struggled with this stuff today...
To me, to create a separate layout just because I want to skip the footer or header seems like much code for little win. If I can stick to the main layout, I can just get at the controller and the action
currently loaded, and have it omitted this way (write this in main.php):
$contr = Yii::$app->controller->id;
$action = Yii::$app->controller->action->id;
$skipFooter = $contr == 'site' && $action == 'login'; //...or enter here what U want
... and then later:
<?php if (!$skipFooter): ?> //Never at login...
<footer class="footer">
<div class="container">
<p class="pull-left">© YourSite.com <?= date('Y') ?></p>
<p class="pull-right"><?= Yii::powered() ?></p>
</div>
</footer>
<?php endif; ?>
I previously asked a question about what options are available to inject a navbar (template) automatically on a per module basis.
Following one of the suggestions I added a view variable in Myapp/Module/Mymodule/Module.php which is the path of the template
public function onBootstrap($e) {
$viewModel = $e->getApplication()->getMvcEvent()->getViewModel();
$viewModel->subNav = 'mymodule/mymodule/subNav';
}
Then in the Application/view/layout/layout.phtml checked for the presence of this variable and displayed it:
<?php if($this->subNav) : ?>
<nav class="navbar navbar-inverse" role="navigation">
<?php echo $this->partial($this->subNav); ?>
</nav>
<?php endif ?>
This works exactly like I want it to, but now I want to pass the template dynamic data from a database.
So my question is this: Its OK to do this in Module.php right? In onBootstrap() I want to call my data model to access my DB and store the results in a view variable. Is this misuse of Module.php? This kind of operation is supposed to be done in a controller, but I want this template to be injected automatically without my controller having to know about it. I'm new to MVC and zend and want to make sure I'm not violated some fundamental design principle.
create a custom view helper , load your data in it and render the your view and use it in the layout
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
class MyNav extends AbstractHelper{
public function __invoke(){
//load your data
return $this->view->render('my nav view script',array('data'=>$data))
}
}
in your modules config file
'view_helpers' => array(
'invokables' => array(
'mynav' => 'Application\View\Helper\MyNav',
),
),
in layout
$this->mynav();
I am building a simple application using Symfony. The page layout consists of a main body with left and right sidebars. The sidebars contains several modules which are user configurable.
Symfony provides slots which seem to be the correct way to fill the sidebars:
layout.php
<div id="left_sidebar">
<?php if (has_slot('left_sidebar')): ?>
<ul>
<?php include_slot('left_sidebar') ?>
</ul>
<?php else: ?>
<!-- default sidebar code -->
<?php endif; ?>
</div>
To fill the slots I tried using a filter. The problem is that some modules in the sidebars depend on what happens in the actions (category updates etc). So they should be generated after the action has run to completion.
msBootstrapFilter
class msBootstrapFilter extends sfFilter
{
public function execute($filterChain)
{
// Generating the sidebars at this point is TOO early
// as the content of some sidebars depends on the actions
// Execute next filter
$filterChain->execute();
// Generate the sidebars after running through all the code
// This is TOO LATE, the layout has been rendered
$this->generateSidebars();
}
}
I do not want to add a "run sidebar" call to each action as that seems inflexible.
What is the best point in the Symfony event flow to generate the sidebar content ? Is there a suitable event that I can connect to?
You can use a component? Which basically is a slot with some sort of action attached to it. In your action you can do whatever your logic needs and render it in the same way as above, but with more logic.
From the manual:
A component is like an action, except
it's much faster. The logic of a
component is kept in a class
inheriting from sfComponents, located
in an actions/components.class.php
file. Its presentation is kept in a
partial. Methods of the sfComponents
class start with the word execute,
just like actions, and they can pass
variables to their presentation
counterpart in the same way that
actions can pass variables.
What is best practice to create dynamic sidebar or other non content layout places with zend framework. At this moment I created controller witch i called WidgetsController. In this controller i defined some actions with 'sidebar' response segment for my sidebar and in IndexController i call them with $this->view->action(); function but I don't think that is a best practice to create dynamic sidebar.
Thanks for your answers.
You question doesn't provide many details. Generally, I'd say load the sidebar as view template, via the render/partial methods of the view. So from inside a view:
//$data is dynamic data you want to pass to the sidebar
echo $this -> partial('/path/to/sidebar.phtml',array('menuitems' => $data));
And then sidebar could process that dynamic data:
//sidebar.phtml
<div id="sidebar">
<?php foreach($this -> menuitems as $item) : ?>
<?php echo $item['title']; ?>
<?php endforeach; ?>
</div>
If you need extra functionality, you could create a dedicated view helper to handle it.
This works for ZF 1.11: A dynamic sidebar implementation in Zend Framework
Yes, folks, my bet is that the answer from the Code Igniter folks is the correct approach.
Actually its helpful to see how CI does it simply, and to make comparisons.
It's basically the same in ZF, except that instead of the "named view" ZF has "Partials".
ZF simply has more imposed discipline, such as layouts + views + partials, and more internal machinery to implement this, which actually does make it run half as fast,
whereas code igniter just seems to have flattened this whole apparatus into "named views".
(I haven't yet made up my mind as to whether ZF has over-cooked it or whether some CI steaks have to remain raw in the middle.)
If you use $navigation->setPartial(blah blah) then the leadup array (technically this kind of data constitutes the "model" part of the MVC thing) and is made available to the partial.
So there you have it, the idea seems to be don't pull the display aspects of the model into the controller, push the model display stuff out to the view processing machinery.
I am just about to have a go at this myself, I did do a search on partials in the view helpers section of the Zend Manual to find this, even though the examples are a bit thin.
Wish me luck
Keith
We'd like some more details, i.e what sort of content is shown in the sidebars, how you show them (i.e using a <ul>, <div> or something else), is the content retrieved from a database, etc.
In CodeIgniter which also uses the Model, View, Controller format, we create a view called 'sidebar.php' and in the Header view, we include a call to this sidebar view, i.e:
<html>
<head>.......</head>
<div id="header">....</div>
<?php $this->load->view('sidebar');?>
The sidebar view contains the logic for showing the menu items. Usually it is a static menu, but if it was dynamic and had to be fetched from the database, I would do this:
<ul>
<?php
$items=$this->some_model->getMenuItems();
foreach ($items as $item):
?>
<li><a href="<?=$item['url'];?>"><?=$item['text'];?></li>
<?php endforeach;?>
</ul>