Small mystery: can't seem to pass views between my Index controller (chartAction) and my view. When I go to my localhost it is not accessing the view phtml- instead it is just showing the controller every time (i.e: if I write "echo "HELLO WORLD!""; in my controller I get that echoed...but if I do a $this->view->test = "Hello World!" then access the index.phtml and type in echo $this->test; I get nothing (it still defaults to the controller action). Is there a step that I'm missing here? Why is my $this->view not functioning? I used the command line to create the view so I'm pretty sure that should be set up correctly.
Do I need to register something? Thanks for any help!
Assuming a standard MVC setup of ZF1.x, there is a definite relationship between the url, the controller and the action.
The url http://mydomain.com/index would call the index action of the index controller, typically the index action is the default action and is called automatically. the view script would be /application/views/scripts/index/index.phtml
The url http://mydomain.com/index/chart would call the chart action of the index controller and the view script would be /application/views/scripts/index/chart.phtml
Keep in mind that this behavior is changeable based on configuration and routing options.
It sounds like you may be fairly new at working with ZF. So something like the following may help demonstrate the relationship :
// application/controllers/IndexController.php
class IndexController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
$this->view->test = "Hello World, from the indexAction().";
}
public function chartAction()
{
$this->view->test = "Hello World, from the chartAction().";
}
// application/views/scripts/index/index.phtml
<?php echo $this->test ?>
// application/views/scripts/index/chart.phtml
<?php echo $this->test ?>
now test your application by calling the url's:
http://yourDomain.com/index/index
http://yourDomain.com/index/chart
If your setup is correct you will see the proper response in your pages.
Case 1: View disabled for just one action:
Look for the following code in your action.
$this->_helper->viewRenderer->setNoRender(true);
Case 2: View disabled for all actions in a specific controller:
Look for the above line in either the init() or preDispatch() functions of the controller.
Case 3: View disabled for all actions in all controllers:
Check case 1 and 2. Also, look for something like the following in your Bootstrap.php:
$frontController->setParam("noViewRenderer", true);
If you find the code like above, you will have to comment it out to get the view working. I am sure there are more possibilities to disable view. Those are to be checked after this.
Your view is disabled..check these lines of code in your action or init of your controller or even the class your controller may be extending
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
UPDATE
You are doing it in your chartAction and echoing in your index.phtml you must do that in your chart.phtml
Related
Let's say I got ControllerA and ControllerB which both implement the same layout. Now I want to pass data to layout, for example, a message that should appear in layout no matter which controller implements it. If I had only 1 controller, I would do something like:
class Controller extends \BaseController {
public function setupLayout() {
View::share('message', 'Hello world!');
}
// further methods
}
However, when I want multiple controllers to implement a layout, I have to do this in every controller, which doesn't sound reasonable. So I wanted to ask, is there any native way in Laravel to pass data to layout and not to copy code in every controller.
Thanks in advance!
For those cases I would recommend a view composer, where you can set them for more than one view (or layout) or just all of them:
View::composer(['store.index', 'products.*'], function($view)
{
$view->with('model', 'one');
$view->with('colour', 'black');
});
You can put that in your routes file, filters file or, like, me, create a app/composers.php and load by adding
require app_path().'/composers.php';
To your app/start/global.php.
In the Layouts folder, I have a layout called signup.blade.php
In my controller, I'm assigning a layout to it like so:
protected $layout = 'layouts.signup';
In a separate folder, named "signup" I have a file called "signup1.blade.php" It contains your typical blade template stuff. It's a section called "content". Before the code I have
#section('content')
and it's got #stop at the end.
My controller looks like this:
public function SignUp()
{
$this->layout->content = View::make('signup.signup1');
}
The frustrating part is that this is working with another layout and controller. I've double checked they're the same, and this one does not work. Any suggestions would be greatly appreciated. Thank you.
So, assuming this controller extends BaseController (it must for $layout to work), the code execution sets $this->layout to View::make($this->layout).
Your error seems to show that $this->layout is not getting set to a View object correctly.
Try to run this and see if $this->layout is an object/class, and if so, what class it is.
public function SignUp()
{
echo gettype($this->layout);
echo get_class($this->layout);
}
Knowing what $this->layout does not get changed into a View object means that the setupLayout() method is either not called or, more likely, not the result of View::make($this->layout) is not a proper view (perhaps it's silently failing for some reason).
The above steps hopefully give you a clue into whats happening there. Perhaps layouts.signup isn't a layout the app is finding?
What do your routes look like?
Change
class UsersController extends Controller
to
class UsersController extends BaseController
Hopefully the author of Confide fixes this :-)
i just created a controller like this:
<?php
class UsuarioController extends AppController {
}
?>
I went to http://urubu.zz.mu/controller/UsuarioController.php to test, and this was the result:
Error: ControllerController could not be found.
Error: Create the class ControllerController below in file: app/Controller/ControllerController.php
}
Theres no class ControllerController in my project.
I found a line in AppController(From CakePHP) :
App::uses('Controller', 'Controller');
But i dont know what it means and how to solve it.
Can anyone help? thanks
TLDR:
Read the wealth of documentation in the Online CakePHP Book to better understand CakePHP.
Accessing a Controller's index() action
In CakePHP, you access the index action of your Controller via the URL like this:
http://www.mydomain.com/usarios
You do NOT need to add "controller" to the URL.
The above would run the index() action within the UsariosController.
Accessing another action within a Controller
http://www.mydomain.com/usarios/register
This would run the register() action within the UsariosController.
Passing variables to actions within a Controller
http://www.mydomain.com/usarios/view/1
This would pass 1 to the register($userId) { action in your UsariosController.
I'm little bit new to Zend, I want to use a controller action through out the entire application automatically, I don't have a clear idea to how to use it, thought about init() method, action helpers, etc.
Then instead of simply creating controller action create controllerAction Helper . Here you can find more about it
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html
My_Helper_Magic extends Zend_Controller_Action_Helper_Abstract
{
public function preDispach()
{
//code inside here will run for entire application automatically
}
}
In your bootstrap do
Zend_Controller_Action_HelperBroker::addHelper(new My_Helper_Magic());
In Response to comment .
It depends upon your 'code fragment' , If your code fragment does not required to know nothing about module , controller, action , base url then you can use Bootstrap init function
like
public function _initAlways()
{
//see how this function name is prefixed with _init hence it will be called by ZF //everytime. You can put your code fragment here
//If your code fragment depends upon some stuff like baseurl then do action controller
// registration here instead
Zend_Controller_Action_HelperBroker::addHelper(new My_Helper_Magic());
}
In Response To comment
You can save any instance of your object inside Zend_Registy and retrieve it whereever you like
Inside Bootstrap.php
public function _initSetup()
{
$object = new My_Custom_Object();
Zend_Registry::set('my_custom_object',$object);
}
Later in your view or controller do
$myObject = Zend_Registry::get('my_custom_object'); //to access it
I want to write a new controller file, for example:
aaa.php
class aaa extends CI_Controller
{
public function bbb()
{
// Stuff
}
}
how can i enter aaa.php's bbb(),
The example files are begin with welcome.php's index() function.
how can I change that to begin with my new controller file?
If you provide nothing to the base URL, CI will always assume you want the index action. Like localhost/foo will call foo's index() action. With localhost/foo/bar, you will call foo's bar() action. If you want to call localhost and you want to access foo's index(), you need to check that $route['default_controller'] = 'foo'; is correctly setup in your config.php. (If that's not working, check the .htaccess and the index.php to add it manually)
You want to have a separate function run as the Controller's default function? Why not just call this separate function from index()? Beyond this, I'm not really sure what you're asking...the CodeIgniter user_guide is rather extensive if you haven't looked through it.
If you want to use the bbb function of the aaa controller, you just enter this in the url:
www.mysite.com/aaa/bbb/
As Gsto said, to call bbb function enter the url: mysite.com/aaa/bbb
If you want mysite.com/aaa to call bbb() instead of index() by default, your going to want to create the _remap() function in aaa.php controller to call bbb() instead.
See: CI Controllers - Functions Docs
The way to access your controller's methods in CodeIgniter is by uri. The default routing is:
example.com/controller/function/param1/
So to access aaa's bbb() method, you should access the following uri:
/aaa/bbb
If you want to set aaa's bbb() method as the default page of you application, there is two things to do.
You must first tell CodeIgniter to set aaa as your default controller
/* /application/config/routes.php */
$route['default_controller'] = "aaa";
After that, the aaa's index() method will be call by accessing your base site url. You can't tell CodeIgniter to change de default method index() to something else (without setting some routes), so the easiest way to call bbb() by default would be that:
/* /application/controllers/aaa.php */
class aaa extends CI_Controller
{
public function index()
{
$this->bbb();
}
public function bbb()
{
// Stuff
}
}