I am learning cakephp from few days ... so please help a bit thanks .
I made a controller --
class PostController extends AppController {
var $name='Posts';
function index(){
$posts=$this->Post->find('all');
$this->set(compact('posts'));
}
in view had post folder with index.ctp file . I want to ask from where it gets "CakePHP: the rapid development php framework" and from where it takes content . I send this data from controller so it prints only var_dump($posts); ... Thanks in advance.
cake php has default layout files in folder "app/view/Layout" .. file name default.ctp in this folder will be taken as default ..
if you open default.ctp in layout you will see something like
<h1><?php echo $this->Html->link($cakeDescription, 'http://cakephp.org'); ?></h1>
and other links defined ..you can comment the and check what changes you get in view.
here is from where view gets ""CakePHP: the rapid development php framework"
this line
<?php echo $this->fetch('content'); ?>
in layout fetch the data from controller to show in view which we set using $this->set() in controller
if you want to change the layout create your own in layout folder
and use in contoller like
class PostController extends AppController {
var $name='Posts';
function index(){
$posts=$this->Post->find('all');
$this->set(compact('posts'));
$this->layout = false; // ot you can set ypur own file like 'xyz' for 'xyz.ctp'
}
to set layout for each action in controller use
function beforeFilter() {
parent::beforeFilter();
$this->layout = 'layout';
}
To learn more about layout see http://book.cakephp.org/2.0/en/views.html#layouts
Hope you got me
"CakePHP: the rapid development php framework"
For above look inside views -> layout -> default.ctp file.
Also change default cakephp routing so you can see posts index action when you have posts in URL.
The best way to call a layout from controller is
$this->layout = 'Your layout page name';
Related
I am very new to SugarCRM (SuitCRM 7.7.8). I could create a controller and could echo some strings in it. I wanted to make that value in a view file. I got confused whether I should use some js files or some tpl view file.
This is my code:
<?php
class MymoduleController extends SugarController {
//Can now put actions here
public function action_convert(){
echo "Hello world!";
//return true;
exit;
}
}
How can I map the controller to a view file.
In your controller action method add the following:
$this->view = 'EditView'
Change the 'EditView' to the view you wish to use. The built in MVC stuff is kept in include/MVC and the include/ListView, include/EditView, and include/MVC/DetailView.
If you take a look at say module/Accounts/views. You can see how views are implemented. It is best to create your code in the custom/modules/[module] folder. As this ensures that your changes will not be overwritten when you upgrade SuiteCRM.
You should separate your html with your view using tpls.
if you add the following in the display method of your view:
function display(){
$template = new Sugar_Smarty();
$template->assign('APP', $app_strings);
$template->assign('MOD', $mod_string);
echo $template->fetch('include/ListView/ListViewGeneric.tpl');
}
you can load your custom views.
I'm new to CodeIgniter. I have a homepage with links. I know how to link the homepage with another page, but I think I'm doing something wrong in the routes.php
I looked through the tutorials, but I still can't find the problem. I tried writing the routes in different ways. Can someone help me?
View of home.php
<ul>
<li>Homepagina</li>
<li>Over</li>
<li>Inloggen</li>
<li>Registreren</li>
<li>Profiel</li>
<li>Matches</li>
<li>Config</li>
</ul>
Controller of home.php
class Pages extends CI_Controller {
public function view($home ='home')
{
$this->load->helper('html');
$this->load->helper('url');
if (! file_exists(APPPATH.'views/pages/'.$home.'.php'))
{
show_404();
}
$data['title'] = ucfirst($home);
$this->load->view('templates/header',$data);
$this->load->view('templates/slideshow', $data);
$this->load->view('pages/'.$home, $data);
$this->load->view('templates/footer',$data);
}
}
routes.php
$route['default_controller'] ='pages/view';
$route['login'] = 'login/view/login';
Controller of Login
class Login extends CI_Controller {
public function view($login ='login')
{
$this->load->helper('html');
$this->load->helper('url');
if (! file_exists(APPPATH.'views/pages/'.$login.'.php'))
{
show_404();
}
$data['title'] = ucfirst($login);
$this->load->view('templates/header',$data);
$this->load->view('pages/'.$login, $data);
$this->load->view('templates/footer',$data);
}
Thank you for the help in advance!
just call controller and method
<li>Inloggen</li>
If not works
<li>Inloggen</li>
try this
When you extend a class that has a constructor, you need to call that constructor from the class you are extending it from... ie
class Controller_name extends CI_Controller {
public function __construct(){
parent::__construct(); // Call the CI_Controller __construct();
}
Note that the __ is actually two underscores but appears a a single line on here.
With your Routes
routes.php
$route['default_controller'] ='pages/view';
$route['login'] = 'login/view/login';
Remember that a CI URL is of the form domainname.com/controller/method
The default_controller is where the browser will be redirected to if you do not supply the controller/method ie - just domainname.com. With your default controller route, you will be redirecting to your pages controller and accessing the view method. Going by your code that should be fine.
Your login route should also be fine if you access domainname.com/login as your url - this will be sent to your login controller, view method and passing in login as the page name.
home.php
<ul>
<li>Homepagina</li>
<li>Over</li>
<li>Inloggen</li>
<li>Registreren</li>
<li>Profiel</li>
<li>Matches</li>
<li>Config</li>
</ul>
With your link to home.php, you might want to create a controller called Home.php and define the index() method. So change
Homepagina
to
Homepagina
The link then becomes domainname.com/home - which points to your home controller and it's index method.
Then we come to this
<a href="<?php echo site_url('login/login');
This will create domainname.com/login/login. Your login route is only looking for domainname.com/login. So the current link domainname.com/login/login is looking for a Controller called login with a method called login.
Your link with myprofile/myuserprofile will be looking for a controller called myprofile and a method called myuserprofile. There is nothing in your routes.php to deal with this. So if that is not working, it's nothing to do with the routes.
So this is all pointing back to you (possibly) not having defined the __construct methods as I discussed at the beginning.
<li>Inloggen</li>
if it works you can remove index.php.
follow this documentation to remove index page
If you don't use full path of url i.e. starting with http[s]://, it's always smart start links with fore slash:
<li>Over</li>
You can use CI url helper and be more safe in case you are struggling™ with .htaccess and index.php with:
echo base_url('pages/about')
Also, practicing static pages tutorial, pay attention on docs' Loader lybrary page, so you can find how to pass all data to all view pages, leaving it to view files and in that case you don't need to set second parameter in here:
$this->load->vars($data);
$this->load->view('templates/header');
$this->load->view('templates/slideshow');
$this->load->view('pages/'.$home);
$this->load->view('templates/footer');
And all $data array will be passed to all view files and you'll be able to make output of those.
Maybe you find it usefull once. ;)
I have single controller and one view page when i try to load this view page using controller I am getting 404 error even route has set.
Controller:
class Navigation extends CI_Controller{
public function index(){
$this->load->view('header');
}
}
In the view directory i had place a file with the name of header and it contains as "Test Navigation" in body that's it.
Routing file:
$route['default_controller'] = "welcome";
$route['404_override'] = '';
$route['menu'] = 'Navigation';
This CI script placed under directory of books. So when i type as localhost/books/index.php/menu , I am getting 404 error. Please explain how to solve this.
Controller class name & controller filename should be same. class Navigation should be under Navigation.php.
Note: This rules applied for the Model too.
I have an existing Zend 1.12 project with modules et al that I am trying to simply add a view to create another web page. So there is a controller called "Management" that has an index action, the default of course, and the correct view page called index.phtml. That is all fine and works inside the entire project/website.
class Management_IndexController extends Default_Controller_Action {
function indexAction() {
$this->view->headStyle()->appendFile('contact.css', 'contact')
->appendFile('message.css')
;
$this->setLayout('management');
$this->view->headTitle()->set('Management');
$message = new Com_Ui_Message();
$request = $this->_request;
$settings=new Com_Data();
$content=new Com_Data();
$data = new Com_Data($request->getPost());
$this->setFocus('#name');
}
function chatAction() {
$this->view->headStyle()->appendFile('contact.css', 'contact')
->appendFile('message.css')
;
$this->view->testMessage = "test";
}
}
So if I browse to http://www.bmcmusicgroup.com/management I see the default action public function, and that is all good. If I try to add another public function called chatAction inside of the management controller, and create a view page called chat.phtml in the appropriate folder where the index.phtml file is (the management index.phtml file), and then point my browser to http://www.bmcmusicgroup.com/management/chat I get nothing.
This is an existing site that I am trying decipher. Any pointers would be greatly appreciated.
Try changing your url to the following and you should see the page: http://www.bmcmusicgroup.com/management/index/chat. The URL you entered (http://www.bmcmusicgroup.com/management/chat) decomposes like so:
Module: management;
Controller: chat;
Action: index
Kind sirs,
I'm using Codeigniter to build a blog. I might need a way to redirect a 404 error into a custom 404 page. Just like what Abduzeedo.com's 404 page. Is it possible to control this by using routes? Or should i use controllers to direct it to another view? Thanks very much!
there is another way which I use: by overriding Exception core class of Codeigniter. Firstly make sure your config file(system/application/config/config.php) subclass prefix is as following
$config['subclass_prefix'] = 'MY_';
Then make a file named MY_Exceptions.php in system/application/libraries. Then override the function show_404() function here as follows.
class MY_Exceptions extends CI_Exceptions{
function MY_Exceptions(){
parent::CI_Exceptions();
}
function show_404($page=''){
$this->config =& get_config();
$base_url = $this->config['base_url'];
$_SESSION['error_message'] = 'Error message';
header("location: ".$base_url.'error.html');
exit;
}
}
Now Error controller will be your error page, where the page will be redirected for 404 error.
Custom 404 page is create by using the following 3 steps,
Step 1: First open routes.php in application/config and set a custom controller name,
$route['404_override'] = 'my404'; //my404 is class name.
Step 2: Create a new controller and write the following code in it. The code is very easy to understand. So I am not going to explain it here,
<?php
class my404 extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->output->set_status_header('404');
$data['content'] = 'error_404'; // View name
$this->load->view('index',$data);//loading in my template
}
}
?>
Step 3: Create a view file with name 'error_404.php' with your custom message.
You don't need to create any controller or modify the code, rather than modify one view file. Just placed your 404 page HTML into the application/views/errors/html/error_404.php file.
If you want to use base_url() or any other CodeIgniter functions, initialize CI_Controller class like the below code.
<?php
$ci = new CI_Controller();
$ci =& get_instance();
$ci->load->helper('url');
?>
For a simple solution, go to your "error_404.php" file in "application/errors/" directory and put this code at the beginning of the file:
header("Location:".base_url());
It'll redirect your 404 page to home page.
Simple Solution.
Create your custom error or Page not found page within view.
Create a controller for error page. Within index function load the view of error page that you already created in previous step.
Now, Go to your application/config/routes.php
you should find $route['404_override'] = '';
Its the '' mean its calling the default error page.
Now place your controller name for error page .
It will work.
Suppose my Controller for error page is 'error'. and view page is 'notfound' .
So, code for Error.php controller will be:
<?php
class Error extends CI_Controller
{
function index()
{
$this->load->view('notfound');
}
}
Now, I replaced $route['404_override'] = ''; to $route['404_override'] = 'error'; in
application/config/routes.php.
Well you want to show your custom 404page when page not found in your website you just replace your custom file on core file which is located in application/error/error_404.php just replace your file here and you are good to go else where you can follow other method by using routes and custom controller for you custom 404 page. But the shortest and easiest way is replacing the core file with custom 404error page file
First open routes.php in the application/config folder and set a custom controller name.
$route['404_override'] = 'custom404'; //custom404 is class name.
Create a new controller and write the following code in it. The code is very easy to understand. So I am not going to explain it here.
<?php
class custom404 extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->output->set_status_header('404');
$data['content'] = 'error_404'; // View name
$this->load->view('index',$data);//loading in my template
}
}
?>
Create a view file with name 'error_404.php' with your custom message.
That is all you need to do and will be having your custom 404 page now.
No need to define any complexity.
The process is very simple.
First, go to your application->config->routes.php file and change this with your controller's function, where you simply load the header, footer, and 404.php page.
$route['404_override'] = 'welcome/_404';
Then come to your controller's page
function _404(){
$this->load->view("header");
$this->load->view("404");
$this->load->view("footer");
}
The same question is also available here
Display a view for 404 error in CodeIgniter
A more simple solution:
Just edit /application/views/error_404.php with your custom 404 page :)