how to make custom CListlView widget in yii - yii

I am developing my web application in the Yii framework . I do not have enough experience in the Yii framework. I want to make the view for the index post page. The Yii provide the CListView for this, but I want to make some customization on that.

You can extend a widget with the following steps:
Copy CListView.php from /(yii root)/framework/zii/widgets to /(application root)/protected/widgets
Rename the file BineshListView.php
Open BineshListView.php. Add this before the class declaration
Yii::import("zii.widgets.CListView");
Change the first line of the class declaration to:
class BineshListView extends CListView { ...
Now, you have your own BineshListView class you can customize. To use it in a view, you can call it like you would CListView
$this->widget('application.widgets.BineshListView', array( 'data'=>$model, etc... ) );
Let me add that BineshListView will inherit all the properties and methods of CListView. Therefore, if you do not need to customize a property or method and want to use the original behavior of CListView, you can delete the property or method from BineshListView.

you no need to customize the ClistView . just simply make changes in the partial view file . which is called by the ClistView.
<?php
$this->widget('zii.widgets.ClistView',arrray(
'dataprovider'=>$your-data-provider,
'view-file'=>'custom-view-file'
));
?>
make changes in custom-view-file.
make sure the custom-view-file in same views folder for the controller.

Related

Umbraco with MVC Controller

I am working on MVC and i started learning Umbraco, I didn't get how to bind the umbraco page with mvc controller get method to show the database values. can anyone suggest any url or video?
Thansk...
What you're looking for is Umbraco route hijacking.
You can read about it here.
https://our.umbraco.org/documentation/reference/routing/custom-controllers
It's easiest to demonstrate with an example : let's say you have a Document Type called 'Home'. You can create a custom locally declared controller in your MVC web project called 'HomeController' and ensure that it inherits from Umbraco.Web.Mvc.RenderMvcController and now all pages that are of document type 'Home' will be routed through your custom controller! Pretty easy right :-) OK so let's see how we can extend this concept. In order for you to run some code in your controller you'll need to override the Index Action.
So, basically, you "simply" need to create a controller named after your document type, so for example, a document type with the name "TextPage" would need a controller called "TextPageController". Now, if you read through the documentation, you'll find that your "TextPageController" will need to inherit from the RenderMvcController. Here's an example how to achieve this.
public class TextPageController : RenderMvcController
{
public ActionResult Index()
{
return View("~/Views/TextPage.cshtml");
}
}
This forum link may help you:
https://our.umbraco.org/forum/developers/razor/38242-Umbraco-MVC4111-Surface-controller-using-an-AJAX-form

How to Call Captcha Action in Yii Framework to create image

I have app in Yii framework and i want to add Captcha in it and i don't want use Yii defaults .. long story short , i found one which works fine without framework so i created an action and i put image creation function in it, but when i call that action i get broken link.
yii/mycontroller/createcaptchaimage
What is wrong in my solution ?
the captha code is in this link .
There is nothing wrong in your solution. You Obviously have some Routing issue . Did you checked your main.php or .htaccess ?
I found this captcha-extended extension more beautiful than CCapthcha. Here is the link:
http://www.yiiframework.com/extension/captcha-extended/
You call it just like normal CCaptcha widget from any view/layout file:
<?php $this->widget('CCaptcha'); ?>
This will look for controller action "captcha" which is defined in array of actions.
public function actions(){
return array(
'captcha'=>array(
'class'=>'CaptchaExtendedAction',
// if needed, modify settings
'mode'=>CaptchaExtendedAction::MODE_MATH,
),
);
}

Invoke action in Yii

This is my scenario. A home page with a upload form, user upload the images and submit, then the controller will catch the data from user, add it in the Model. Finally the image info will display in the another page.
I put the actionIndex() in the siteController to render the home page, the actionUpload() in the same file to handle the data user. So in the view, what should I put in the form action to invoke the actionUpload(). I think the flow is quite weird in Yii when I read the blog demo code, I just follow same way with the ASP.NET MVC. Suggest me the right way, plz. Thanks
Depends on how you build your form. If using CHtml, do the following:
<?= CHtml::beginForm($this->createUrl('site/upload'))?>
If you have a model that sits behind it:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'action' => $this->createUrl('site/upload'),
)); ?>
Please check the Yii documentation and examples on how to properly set up forms. You have several choices here.

separate header, content and footer in yii

I have a login form in my header section of the website. If user is logged in than insted of the login form user profile details will be shown. The question is how to separate header footer and content into different views and call them from one controller? Or maybe there is another solution...Thanks for help.
In your header view you could write something like this.
<?php if(Yii::app()->user->getId()): ?>
<?php $this->renderPartial('//world/_header_user')); ?>
<?php else: ?>
<?php $this->renderPartial('//world/_header_guest')); ?>
<?php endif; ?>
Using the Model-View-Controller (MVC) design pattern, the look of a Yii-based site is naturally controlled by the View files. These files are a combination of HTML and PHP that help to create the desired output. Specific pages in a site will use specific View files. In fact, the View files are designed to be broken down quite atomically, such that, for example, the form used to both create and edit an employee record is its own file, and that file can be included by both create.php and update.php. As with most things in OOP, implementing atomic, decoupled functionality goes a long way towards improving reusability. But the individual View files are only part of the equation for rendering a Web page. Individual view files get rendered within a layout file. And although I’ve mentioned layouts a time or two in my writings on Yii, it’s a subject that deserves its own post.
To be clear, layouts are a type of View file. Specifically, whereas other View files get placed within a directory for the corresponding Controller (i.e., the SiteController pulls from views/site), layout files go within views/layouts. But while the other View files are associated with individual Controllers (and therefore, individual pages), layouts are communal, shared by all the pages. Simply put, a layout file is the parent wrapper for the entire site’s templating system. I’ll explain
ypu can see more details
http://www.larryullman.com/2012/05/16/working-with-layouts-in-yii
The easiest way is probably to use a different layout, which you just switch on login. If not, showing partials / components based on Yii::app()->user->isGuest also works well.
Your default generated Yii application has a parent Controller in protected/components/Controller.php.
If you need to access additional parameters in layout, add public properties to Controller, set them in your child controller, and use them in your view/layout files.

Kohana 3: How to provide API functions in template/view like WordPress?

I'm working on a project which allows the advanced user to define their own way of showing the information and access some basic API. For example, I provide a show_search_box() function so that the user can call this function in the view file when they want to show the standard search box, or they could call the function with parameters to customize the search form.
e.g. this code in the template will show a search form with watermark text "Enter keyword here".
<div><?php show_search_box('Enter keyword here'); ?></div>
What I'm thinking actually is exactly like what WordPress does in its template tags. (http://codex.wordpress.org/Stepping_Into_Template_Tags)
My idea is to create a class that provide all those API functions and pass an object instance of the class to the view file, so users can call the API functions in view like:
<div><?php $API->show_search_box('Enter keyword here'); ?></div>
I think it will work, (but have not tested it yet), but I prefer providing a set of direct called functions just like WordPress. What's the best way to do this with kohana 3?
======Update: I have tested the method of pass $API object to view, and it works as expected.
class API {
public function show_search_box($watermark){....}
}
In the controller, pass the $API to the view/template
public function action_index()
{
$this->template->API = new API();
}
Then call the function inside view/template as described above.
Unlike those controller methods, $API cannot access the controller's variables unless they're explicitly assigned: e.g. $API->setVar('VarName', $a_controller_variable), which is quite tedious i think.
Well, unlike Kohana 2.3, views don't execute in the controller namespace, so you can't simply do $this->something().
If you have all your functions in one Model, let's call it API, then you could do this in the view (or base controller if you want it available everywhere)...
$this->template->internalView = View::factory('your_view')
->set('API', Model::factory('API));
(assuming you have a <?php echo $internalView; ?> in a parent view).
Then you could do in your view...
<div><?php $API->show_search_box('Enter keyword here'); ?></div>
Which will run your method on your model. Views shouldn't really know about the existence of models, but your case ma be an exception. Perhaps you could use a helper sort of class instead of a model, if you are worried about breaking the MVC paradigm.
If you want to do what WordPress does (have a bunch of global functions, which I don't recommend), then you will need to define them somewhere. Kohana doesn't really have an easy spot to place them, as it doesn't really cater for a bunch of global functions.