Putting additional pages on kohana framework - php

I've started reading kohana documentation but didn't really understand, the index page I need to define in controllers like public $template = "index"; but how do I add other html, php files as links? Because it doesn't find them if I simply put them in the view's folder.

if you need to create a new page first you need to create a function in controller. also you need to assign the view file in that function.
For ex,
Here i ll create a user login page in user controller.
<?php
Class Controller_User extends Controller_Welcome
{
/**For get User Login page**/
public function action_login()
{
$view= View::factory('login');
echo $view;
}
}
?>
login.php file is placed in application/views/login.php
Now your login page is called in the url like http://mysite.com/user/login/
If you want to call the page in a common template file , first you need to assign template file. then you can easily call the page in template file.
For ex,
user.php controller:
<?php
Class Controller_User extends Controller_Welcome
{
/**For get User Login page**/
public function action_login()
{
$this->template='template.php';
$view= View::factory('login');
$this->template->content = $view;
}
}
?>
template.php view file:
-- your html datas here --
<?php
echo new View("header");
?>
-- your html datas here --
<?php
echo $content; ?>
-- your html datas here --
<?php
echo new View("footer");
?>
-- your html datas here --
Here header is header.php, footer is footer.php. all these files are placed in applications/views/ folder.

public $template = "index" means that Controller_Template class will load view from views/index.php file. You can add requred links directly to this file or dynamically - with template variables or subtemplates.
This wiki may help: http://kerkness.ca/kowiki/doku.php

Related

Yii2 custom widget assets not registering

I'm trying to develop a custom widget, but I've run into a wall. I have some third-party JS/CSS files I need to include, but for whatever reason, Yii2 will not register them. I've followed everything in the Yii2 documentation but seem to get nowhere. My directory structure is as follows:
components
- DildenFeedback.php
- DildenFeedbackAsset.php
- views/
-- feedback.php
- assets/
-- feedback.min.js
-- feedback.min.css
DildenFeedback.php (Widget Class)
<?php
namespace app\components\yii2feedbackwidget;
use Yii;
use yii\base\Widget;
use app\components\yii2feedbackwidget\DildenFeedbackAsset;
class DildenFeedback extends Widget
{
public function run() {
parent::run();
DildenFeedbackAsset::register($this->view);
return $this->render('feedback');
}
}
DildenFeedbackAsset.php (Widget Asset Class)
<?php
namespace app\components\yii2feedbackwidget;
use yii\web\AssetBundle;
class DildenFeedbackAsset extends AssetBundle {
public $sourcePath = 'assets/';
public $css = ['assets/feedback.min.css'];
public $js = ['assets/feedback.min.js'];
public $depends = ['yii\web\JqueryAsset'];
}
Feedback View
?>
<script type="text/javascript">
$.feedback();
</script>
views/layouts/main.php (The main template I was trying to call in)
<?php $this->endBody() ?>
<?php
echo DildenFeedback::widget();
?>
So when I go to inspect the page, feedback.min.js/css are nowhere to be found. The view file is rendered correctly, but my guess is that my AssetBundle is improperly formed. Any help would be much appreciated.
EDIT (From the Yii Debugger Assets):
sourcePath /var/www/public/components/yii2feedbackwidget/assets
basePath /var/www/public/web/assets/7e80049a
baseUrl /assets/7e80049a
css assets/feedback.min.css
js assets/feedback.min.js
depends yii\web\JqueryAsset
EDIT 2
I have tested this on a fresh install of Yii2, and I get the same error.
Well, you should simply correct your sourcePath :
class DildenFeedbackAsset extends AssetBundle {
public $sourcePath = '#app/components/assets/';
public $css = ['feedback.min.css'];
public $js = ['feedback.min.js'];
public $depends = ['yii\web\JqueryAsset'];
}
You should also fix your view by using registerJs() instead of a script tag :
$this->registerJs('jQuery.feedback();');
I've solved it, and I feel a bit silly. What it came down to, was the location of the widget call. As I noted in my original question, I was echoing the widget like so:
<?php $this->endBody() ?>
<?php
echo DildenFeedback::widget();
?>
However, I did not pay attention to where that call was. It NEEDS to be before the endBody call
<?php
echo DildenFeedback::widget();
?>
<?php $this->endBody() ?>
This ended up solving the issue. Thanks to Soju for directing me towards the widget call.

Load a single module of page in views codeigniter framework

I am developing a web app using codeigniter framework. My application has a fixed header and footer.I want to middle section (ie the body) of my application to load when the user goes to various pages which are available to him and make the header and footer constant.
(hackerrank.com ... I was talking about a website similar to this one ... after login into this website ... the header and the sidebar of this remains constant and they load the remaining page ... how can we implement it using CI framework)
Is there any way through which I can achieve this?
I am performing the following actions which is making me to load the complete website(Its like reloading the entire page :/)
As You can see the following code is present in my template.php
<?php
$this->load->view("templates/header.php");
$this->load->view($main_body);
$this->load->view("templates/footer.php");
?>
and I Controller I write the following piece of code usually
public function load_page(){
$data['main_body'] = 'dashboard_pages/dashboard_view';
$this->load->view('template.php',$data);
}
You were pretty close to the solution: Load the header and the footer as they are, variables:
<?php
$this->load->view($header);
$this->load->view($main_body);
$this->load->view($footer);
?>
The tricky thing, is that you're always writing the method function load_page() in every controller you write, and it would be better having a MY_controller, a class previous to your controller class in which you'll write which footer you're referring:
Check for writing a MY_Controller here: http://ellislab.com/codeigniter/user-guide/general/core_classes.html
Then, write your MY_Controller:
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function load_page( $page )
{
$data['main_body'] = $page;
// Logic for your header or footer deppending on logging or whatever...
if ( 1 ==1 ) {
$data['header'] = "templates/header.php";
$data['footer'] = "templates/footer.php";
}
$this->load->view('template.php',$data);
}
}
and you'll have to make sure your controllers extends MY_Controller class, and add a load_page method to whom you'll pass the argument:
class Custom_page extends MY_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load_page( 'dashboard_pages/dashboard_view' );
}
}
I think with that you'll have exactly what you look for, that way you only have to write in one place the logic for the header and footer: In MY_Controller, and just use it in any part.

loading contents of PHP file in MVC architecture

So I'm using Codeigniter and I was wondering how I can load in the contents of my view directly into a calling JS file.
I have a view called "form_layout" which contains PHP code to populate form fields.
$('#wizard').smartWizard
({contentURL:'views/form_layout.php?action=1',
transitionEffect:'slideleft',onFinish:onFinishCallback});
but when I do that I get a server 500 error.
Do i have to route through the controller like?
<?php
class Form_manager extends CI_Controller {
public function index()
{
$this->load->view('form_template');
}
}
?>
and do contentURL:Form_manager/index?
Try This
var SITEURL = 'yourdomainname'; //like www.mysite.com
$('#wizard').smartWizard({
contentURL: SITEURL + '/Form_manager/index/action/1',
transitionEffect:'slideleft',onFinish:onFinishCallback
});
<?php
class Form_manager extends CI_Controller {
public function index($action )
{
echo $this->load->view('form_template','',true);
}
}
?>
Yes, you do have to route through a controller. CodeIgniter does not allow you to short-circuit routes to views directly (unlike Laravel). So, you will have to set up the controller as you show and change the url in your ajax call to site_url('form_manager'). You don't need to specify /index since that is the default function that CI calls when none is specified in the URL.

App/Views/Pages/used.ctp cannot be read (CAKEPHP)

Good day!
I have a home page wherein there's a menu at the left side part of it. The button's href when clicked will go to a .ctp file called used.ctp under app/views/pages FOLDER
I already considered putting pages_controller.php and app_controller.php in my app/controllers folder but nothing happened.
The header tag inside the used.ctp cannot be read.
Only the static header declared from index.ctp is all i can see while viewing the used.ctp
First, there was a mysterious error that appeared like...
Error: Create the class controller blah blah blah user_controller.php below the file
Something like that.
I fixed it by creating the said used_controller.php
<?php
class UsedController extends AppController {
var $name = 'Used';
}
?>
But upon refreshing the used.ctp
it still appears empty with only the static header on top declared on index.ctp
Please help me. :(
If you want to see the full source code:
Download -> http://www.mediafire.com/?h3sc14gogq9ohtc
THANKS
The users.ctp file needs to go in /View/Users and you need to create a method in the controller to link to that view.
So
<?php
class UsedController extends AppController {
var $name = 'Used';
public function users() {
}
}
?>
I suggest following a tutorial like the following;
http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/blog.html

How to make a proper index page for subdirectories in CodeIgniter

I'm building an app that has a section for consumers and businesses, and I want to separate the controllers folder appropriately, so it looks like this -
http://domain.com/users/signup/
http://domain.com/business/signup/
I have it working by creating a separate folder for each section in the "controllers" folder, but I want to know how to make an appropriate page when the user visits the http://domain.com/users/. It currently just loads the homepage. How can I fix this?
You don't need to put them in separate folders for this to work.
File system/application/controllers/users.php:
<?php
class Users extends Controller {
function index() {
// this function will be called at http://domain.com/users
}
function signup() {
// this function will be called at http://domain.com/users/signup
}
}
?>
File system/application/controllers/business.php:
<?php
class Business extends Controller {
function index() {
// this function will be called at http://domain.com/business
}
function signup() {
// this function will be called at http://domain.com/business/signup
}
}
?>
If I understand your question correct, you might be looking for URI Routing. I don't see why you would need folders for this. Simply just create the routes you need and make use of the index methods in the controllers for making the "homepage" for both users and business.

Categories