i am new in the cakephp2x and i dont know how to named the controller and view and model files in the cakephp 2x.
for the simple Event module i have
contoller : EventsController.php
model : Event.php
vide : Events ( folder Name )
now this look like http://example.com/events
Now my i want the link look like this http://example.com/itemsbidders
for the above url i have named the file as below
controller : ItemsBiddersController.php
model : ItemBidder.php
view : ItemsBidders
but i am getting 404 page not found Error in this page.
so can you tell me my mistake. advice me for this issue
Thanks in advance
With HABTM link tables you order model names alphabetically and first model kept plural:
Controller : BiddersItemsController.php
Model : BiddersItem.php
View : BiddersItems
And URL now looks like this: http://example.com/bidders_items (with underscore to separate models).
Remember, first letter capitalised and singular for folders: Controller / Model / View
Now to get URL you want above, you will need to create a router declaration in your Config/routes.php file like:
Router::connect('/itemsbidders',
array('controller' => 'bidders_items', 'action' => 'index'));
Related
we are using subdirectories in our projects no separete views and controllers but in models we didn’t learn yet. Recently I’ve found this https://github.com/cakephp/cakephp/issues/60451 and actually routes and plugins we are already using, we just want to separete our models like this:
Model
-Entity
–Financial
—Money.php
-Table
–Financial
—MoneyTable.php
I’ve tryed put like this then controller is not able to find his model. How can I do to organize it, and make it work?
Things that we've tried:
Use $this->setAlias('TableModel');
Call in controller:
$this->TableModel = $this->loadModel('Subfolder/TableModel');
didn't work for SQL build, and other classes.
CakePHP uses the TableRegister to load models. That class can be configured to use a class that implements the LocatorInterface, and CakePHP uses the TableLocator as the default.
The only thing you can do is configure your own LocatorInterface instance in your bootstrap.php. You would have to create your MyTableLocator and have it change the className for tables to point to subdirectories. What rules for this class name rewritting are used is purely up to you.
bootstrap.php:
TableRegister::setTableLocator(new MyTableLocator());
MyTableLocator.php:
class MyTableLocator extends TableLocator {
protected function _getClassName($alias, array $options = [])
{
if($alias === 'Subfolder/TableModel') {
return TableModel::class;
}
return parent::_getClassName($alias, $options);
}
}
The above isn't working code.
I'm just demonstrating what the function is you need to override, and that you need logic in place to return a different class name.
You can check if the $alias contains the / character, and if so. Return a class name by extracting the subfolder name from the $alias. Take a look at the TableLocator to see how it's using the App::className function.
Controller Name : SaleController.php
action name : index.ctp
When I write localhost/cakephp/Sale/index is written in address bar,
index page of SaleController is shown.
When I write localhost/cakephp/Sale/ is written in address bar,
index page of SaleController is shown.
Now,problem is I don't want to go index file when localhost/cakephp/Sale/ is written.
My cakephp version is 2.5.7.
If know ways,help me plz.
there is a lot of solutions :
Select view in the controller
in your SaleController.php
function index(){
$this->view="nameview.ctp";
}
Routes :
Router::connect(
'Sale/',
array('controller' => 'Sale' , 'action'=>'youraction' ),
array('option' => 'matchingRegex')
);
Select an other action in index
function index(){
$this->actionName();
}
First of all, your controller name should be SalesController (plural s).
Second, action is a method within controller not a file (here index mehod).
Third, index.ctp is a view file.
Fourth, you can change view in action method with render function (e.g. $this->render('other.ctp'); )
Fifth you can have custom URLs with routes.
And last but not least! you've not grabbed a good understanding of the framework, It's better to restudy the documentation.
I have two tables: Ingredients and Customers. The relationship between them is that Customers hasMany Ingredients. By default when doing the cakebake using the console, the only way to change them is by assigning an ingredient to the customer in the Ingredients page. However, I want to have in Customers page a checkbox list of Ingredients that can be assigned. Is it possible to do this? If yes, how?
edit:
What I have done until now is that I add this code to my add.ctp:
echo $this->Form->input('Ingredient',
array('label'=>'',
'type'=>'select',
'multiple'=>'checkbox',
'options'=>$ingredients));
However, it gives me "Undefined variable: ingredients" error when I tried to open the add view.
You want and need a HABTM relationship. Different customers can access and use the same ingredients. Look at the docs here yours would be very similar to different Posts using same Tags.
If you are getting this error:
"Undefined variable: ingredients"
It sounds like you haven't declared this variable in your controller, and set it so that the view can use it. Without knowing your code, you would probably need do do something like this (please note I am guessing what your application structure looks like and have not tested this code).
CustomersController.php
// The controller action for your view
public function view() {
// Get the ID and name of all your ingredients
$ingredients = $this->Ingredient->find('all', array(
'fields' => array('id', 'name'),
'order' => 'name',
'recursive' => -1
));
// We will use this array to store all the HTML select options
$ingredientOptions = array();
// Loop through all the ingredients and add them to the select
// options in a format that is suitable for CakePHP to use in
// the view to build your HTML select menu.
foreach ($ingredients as $i) {
$ingredient = $i['Ingredient'];
$ingredientOptions[$ingredient['id']] = $ingredient['name'];
}
// Make the variable available to the view
$this->set('ingredients', $ingredientOptions);
}
I have a strange problem with CakePHP
CakePHP gives an error on the following line:
View/Designer/cards:
$this->JsBridge->set('Card.DISPLAY_TYPE_FOLDER_GREETING', Card::DISPLAY_TYPE_FOLDER_GREETING);
Class 'card' can not be found.
However in DesignersController I load the model Card via de following line:
public $uses = array('Designer', 'Card');
If I add the following line in the top of DesignersController
App::uses('Card', 'Model');
The page loads, but the following line does not work:
$this->paginate = $this->Card->getPagination($filter);
I have put the code for the model Card.php here : http://pastebin.com/U7zxKHCx
Can you tell me what is going wrong?
Thank you!
Are you including the CardModel in your Controller?
$uses = array('....','Card',....);
Controller properties, classes attached etc. are NOT directly accessible in a View. You will need to set this constant Card::DISPLAY_TYPE_FOLDER_GREETING to a variable:
$this->set('variableName1', Card::DISPLAY_TYPE_FOLDER_GREETING);
Then use it in the View:
$this->JsBridge->set('Card.DISPLAY_TYPE_FOLDER_GREETING', $variableName1);
i want change my url in cake php /eng/users/ to /users/; i mean when i entered this /eng/users/ cakephp don't show me error like this
*Missing Controller
Error: EngController could not be found.
Error: Create the class "EngController" below in file: "app\Controller\EngController.php" *
both this /eng/users/ and /users/ become same ? any solution?
You are looking for Router.
Read this article: http://book.cakephp.org/2.0/en/development/routing.html#routes-configuration
and especially for the cause of users ur you should add in your /app/Config/routes.php
Router::connect(
'/eng/:controller/*', array('controller' => 'users')
);
the eng can be set to some prefix and so on.