I am following this tutorial http://book.cakephp.org/2.0/en/development/rest.html to get up and running with REST in my CakePHP application.
I have added the following to my routes.php file:
Router::mapResources(array('fantasyPicks', 'fantasyPlayers'));
Router::parseExtensions();
In each of my controllers, I have included the RequestHandler component and also setContent to json in the beforeFilter() callback. It looks something like this:
class FantasyPicksController extends AppController {
public $components = array('RequestHandler');
public function beforeFilter() {
parent::beforeFilter();
$this->RequestHandler->setContent('json','text/x-json');
$this->layout = 'json/default';
}
public function index() {
$fantasyPicks = $this->FantasyPick->find('all');
$this->set('json', $fantasyPicks);
$this->render('/json/data');
}
...
My json/data view simply echos json_encode:
<?php echo json_encode($json); ?>
After all of this, navigating to /fantasyPicks/view/1 works as expected. However, /fantasyPicks/1 is giving me the following error:
Missing Method in FantasyPicksController
Error: The action 1 is not defined in controller FantasyPicksController
Error: Create FantasyPicksController::1() in file: app\Controller\FantasyPicksController.php.
<?php
class FantasyPicksController extends AppController {
public function 1() {
}
}
Does anyone know what I am doing incorrectly? Any help is appreciated!
You have to use proper controller naming conventions when accessing the page.
http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html
Refer to the URL considerations section. So you should be going to /fantasy_picks/1 and it will work properly.
Related
I'm using Codeigniter v4 and wanted to show a custom view to users, so I made a Controller named Test:
<?php
namespace App\Controllers;
class Test extends BaseController
{
public function index()
{
$this->load>model('Usermodel');
$data['users'] = $this->Usermodel->getusers();
return view('custom');
}
}
And a Model named Usermodel:
<?php
namespace App\Models;
class Usermodel extends CI_Model
{
public function getusers()
{
return [
['firstmame'=>'Mohd','lastname'=>'Saif'],
['firstname'=>'Syed','lastname'=>'Mujahid'],
['firstname'=>'Mohd','lastname'=>'Armaan']
];
}
}
And the view custom.php already exists in the Views folder.
But when I load the url http://localhost/ci4/public/index.php/test I get 404 Not Found error message.
Also I tried http://localhost/ci4/public/index.php/test/index but shows the same message.
So how to load this method from the custom controller class in Codeigniter v4 properly?
Except you're not parsing the $data to the view (you should do that adding a second parameter to view('custom', $data)), the code doesn't seem to be the problem: When a view could not be loaded in CI, it shows a specific error message (CodeIgniter\View\Exceptions\ViewException), not a 404 Not Found message.
Probably the problem is in another part of your project.
Try this
<?php
namespace App\Controllers;
class Test extends BaseController
{
public function index()
{
$this->load>model('Usermodel');
$data['users'] = $this->Usermodel->getusers();
return $this->load->view('custom',$data);
}
}
add the $data array to your views
controller.php
<?php
define('_root',$_SERVER['DOCUMENT_ROOT']);
include(_root.'/innoshop/application/models/model.php');
// include_once 'model.php';
class Controller {
public $model;
public function __construct()
{
$this->model = new Model();
}
If i put localhost:8888/projectname, i got error like this
404 Page Not Found
The page you requested was not found.
anyone help me
As the guys say you should read the docs as this is very wrong. To fix do this..
class Controller extends CI_Controller{//better to give your controller a more meaningful name
public function __construct(){
parent::__construct();
//use the CI loader - the model will then be available like this $this->model->some_function();
$this->load->model('model');//better to give your model a more meaningful name as well
}
//the index method allows you to use just the controller name in your URI eg localhost:8888/projectname/index.php/controller
public function index(){
echo 'something to see';
}
//an alternative controller method get it like localhost:8888/projectname/index.php/controller/your_method_name
public function your_method_name(){
echo 'something to see in your method';
}
}
If you want rid of the index.php in the URI search for questions related to .htaccess in CodeIgniter
If you want to be able to use a uri like this localhost:8888/projectname then you need to add a route in config/routes.php that defines the default controller like this $route['default']='controller';
I am trying to use the renderPartial method within a action class.
The method is unable to find the view.
The view is within the root of the app /views/invoice/view.php
<?php
class InvoiceAction extends CAction {
public $invoice_id = 0;
public function run() {
Yii::app()->controller->renderPartial('/invoice/view');
}
}
Switching to the following code will work fine:
Yii::app()->controller->renderPartial('//invoice/view');
Switched to the following code and it worked :
Yii::app()->controller->renderPartial('application.views.invoice.view');
I'm trying to populate the select box with countries' timezones. I have seen examples and answers here, but nothing works out for me. I am working on Cakephp 2.3.
My timeZone helper class is in this directory
App/View/Helper/TimeZoneHelper.php.
Here is my controller:
class TimeZoneController extends AppController{
public function index(){
$helpers = array('TimeZoneHelper');
}
}
my view
<?php
echo $timezone->select('timezone');
?>
It isn't working, and I don't know how it works because I have never used this functionality before.
You are working with cake2.x. But you are using 1.x syntax.
The correct syntax for 2.x is:
echo $this->Timezone->select('timezone');
(instead of $timezone-select)
Its also in the documentation by the way:
http://book.cakephp.org/2.0/en/views/helpers.html#using-helpers
When you want to add helper in action you need to use following code
class TimeZoneController extends AppController{
public function index(){
$this->helpers[] = 'TimeZoneHelper';
}
}
If you want the helper to available to all the action
class TimeZoneController extends AppController{
public $helpers = array('TimeZoneHelper');
public function index(){
}
}
If you want the helper to be available to all controllers then you need to add it to /app/Controller/AppController.php
class AppController extends Controller{
public $helpers = array('TimeZoneHelper');
}
I'm new to php, MVC and Zend Framework. I need to redirect to a controller/action via a hyperlink. I used this, as I have used it in a form to redirect to a controller. <a href="mycontroller/myaction"> But this time, it didn't work.
my controller is a very basic controller, where I only tried to dispaly the view conneced to the controller.
Here it is:
<?php
class NewUserController extends Zend_Controller_Action {
public function init()
{
/* Initialize action controller here */
}
public function newuserAction()
{
$this->view->newuser;
}
}
?>
I searched for answers and every answer was similar to what I have done.
please help me to sort this out.
Thanks in advance
Charu
First change your controller name NewUser to Newuser and try to pass url like this :
<?php $url = $this->url(array("controller" => "Newuser", "action" => "newuser")); ?>
test
And in controller :
<?php
class NewuserController extends Zend_Controller_Action {
public function init() {
/* Initialize action controller here */
}
public function newuserAction() {
$this->view->newuser;
echo "----In newuserAction()----";
}
}
?>
Hope it will work for you.