i have a controller called "Movies" so the link is "localhost/Movies" and have some actions in this controller like "localhost/Movies/view/[id]" , "localhost/Movies/view/category/[id]" if i want to make a path by the category name and sub category name like "localhost/Movies/English/new" .
how can i do something like this in cakephp 2. my project now like "localhost/English/new" but i want to put Movies in this path, to make it more fixable, if i want to make a new category just add a column in my database .
thanks
If i understand, maybe by creating a custom route for your action:
In app/config/routes.php:
Router::connect('/movies/:category/:subcategory', array('controller'=>'movies','action' => 'index'));
And you can retrieve the value in your controller with:
echo $this->params['category'];
echo $this->params['subcategory'];
You can also read about it in the cookbook http://book.cakephp.org/1.3/view/945/Routes-Configuration
The default routing in CakePHP is as follows:
mydomain/controller/action/param1/param2/param3/param4/...
If you want to add filter options to an action, just add optional parameters to the action.
function index($category = null, $subcategory = null) {
if(isset($subcategory)){
//will execute if you pass both arguments
}else if(isset($category)){
//will execute if you pass one argument
}else{
//will execute if you pass no arguments
}
}
EDIT
In this case, $category is param1, and $subcategory is param2. The overloaded function can receive either 1, 2, or no arguments. If this is in, for example, the ObjectsController, all these are valid URLs:
localhost/objects/index/ //$category==null, $subcategory==null
localhost/objects/index/foods/ //$category=='foods', $subcategory==null
localhost/objects/index/foods/green/ //$category=='foods', $subcategory=='green'
This allows you to control many options in the same action.
Related
How can I prevent links automaticly from beeing displayed in the Template ctp files?
I will give you an example:
User(id = 1) is allowed to see teamcalendars/view/1
User(id = 2) is not allowed to see teamcalendars/view/1.
User1 is member of the team 1 and should see and follow the link. User2 is not member in any teams and neither should see the link to the calender nor follow it. But I would like to place the link in the teams/index file where both users can go to and see all teams, but with different options per team.
If User2 follows the link (or types it into the browser manually), the controller will return a redirect and a error message about missing privileges. User2 will anyhow never get there. But how do I prevent cake from displaying the link for User2 (its missleading)?
Is there a possibility to connect the link to the controller and action and the id of the object where it is leading to, so I don't neet to take care of building and passing variables for each view just to decide which links can be displayed?
Sorry for not providing any code, but I think anyone knows how to send an array from the Controller to the View and how to validate it with if(){echo $this->Html->link()}, which is what I am doing currently.
Thank you for any help or remarks in advance.
One option is to define your own HtmlHelper and override the link function, such that it checks the permissions on the link first and only outputs it if they are allowed access. Something like the following:
namespace App\View\Helper;
use \Cake\View\Helper\HtmlHelper;
// Or, if you're already using a third-party HTML helper, something like
// use BootstrapUI\View\Helper\HtmlHelper as HtmlHelper;
class MyHtmlHelper extends HtmlHelper
{
function link($title, $url = null, array $options = [])
{
if (checkMyPermissions($url)) {
return parent::link($title, $url, $options);
}
}
}
And then in your AppController:
use App\View\Helper\MyHtmlHelper;
public $helpers = [
'Html' => ['className' => 'MyHtmlHelper'],
// ... and all your other helpers
];
I have a model class Event which has the following actions in question: view and delete.
The deletion can only happen from the view action. Getting to the view action is possible from two places that are:
events/dashboard
and
calendar/view_calendar
the latter takes three parameters: user_id, month and year so that would for example be
calendar/view_calendar/120/5/2014
So depending on which action user got to for example events/view/1400, he has to be redirected accordingly.
The referer does not work as it redirects to the events/view with the id of event which has already been deleted.
Any help or guidance is much appreciated.
Put a second Parameter to the view, like
events/view/1400/0
for dashboard
and
events/view/1400/1
for view_calendar
and pass these params to your delete action, like
events/delete/1400/1
In your delete function you can you use the usual redirect:
if($secondParam == 1) {
$this->redirect(array('action' => 'view_calendar'));
} else {
if($secondParam == 0) {
$this->redirect(array('action' => 'dashboard'));
}
}
You can save somewhere the information about the referrer when you land in events/view and the pass it to events/delete.
You can use a named parameter or a query string so your link to the delete action becomes appear something like
events/delete/1400/referrer:dashboard
or
events/delete/1400/referrer:view_calendar
or
events/delete/1400/?referrer=dashboard
I want to pass values from one controller to another.
For example I have a Conference controller and I want to create a new Event.
I want to pass the Conference id to the event to make sure those two objects are associated.
I would like to store in the ivar $conference using beforeFilter method.
Here is my beforeFilter function in the Events controller
public function beforeFilter() {
parent::beforeFilter();
echo '1 ' + $this->request->id;
echo '2 ' + $this->request['id'];
echo $this->request->params['id'];
if(isset( $this->request->params['id'])){
$conference_id = $this->request->params['id'];
}
else{
echo "Id Doesn't Exist";
}
}
Whenever I change url to something like:
http://localhost:8888/cake/events/id/3
or
http://localhost:8888/cake/events/id:3
I am getting an error saying that id is not defined.
How should I proceed?
when you're passing data via url you can access it via
$this->passedArgs['variable_name'];
For example if your URL is:
http://localhost/events/id:7
Then you access that id with this line
$id = $this->passedArgs['id'];
When you access a controller function that accepts parameters through url, you can use those parameters just as any other variable, like for example say your url looks like this
http://localhost/events/getid/7
Then your controller function should look like:
public function getid($id = null){
// $id would take the value of 7
// then you can use the $id as you please just like any other variable
}
In the Conferences Controller
$this->Session->write('conference_id', $this->request->id); // or the variable that stores the conference ID
In the Events controller
$conferenceId = $this->Session->read('conference_id');
Of course, on top you need
public $components = array('Session');
I am trying to get the user profile loaded with entity_metadata_wrapper:
$user_profile = entity_metadata_wrapper('user', $uid);
dpm($user_profile);
but I get nothing back. The user fields load fine with user_load($uid); What am I doing wrong?
The information provided by Linas is provably incorrect. The use of entity_metadata_wrapper with an entity id is completely okay. The function uses uid internally only if you pass an object to it to begin with, otherwise it passes the argument directly to EntityDrupalWrapper.
Eventually the set method will be called, which states: "Overridden to support setting the entity by either the object or the id."
This basic code will demonstrate a working call to entity_metadata_wrapper with just a user id.
$wrapper = entity_metadata_wrapper('user', 1);
dpm($wrapper->value());
The output will be an array of all the data belonging to the admin user.
The problem you are having is unrelated to your arguments being passed in (assuming $uid is valid), and requires more information to troubleshoot.
entity_metadata_wrapper expects second parameter to be an object, in this case, user object. According to your variable naming, it seems that you are passing user id instead. You can use user_load to fetch user object by ID.
I have the same problem the code not work return empty (Object) EntityDrupalWrapper
global $user;
$user_profile = entity_metadata_wrapper('user', $user->uid);
dpm($user_profile);
But that code work well and return (Object) stdClass with all users fields like name password and other
global $user;
$user = user_load($user->uid);
$wrapper = entity_metadata_wrapper('user', $user);
dpm($wrapper->value());
Only pass the two parameter of controlle in action?
mysite.com/myController/myAction/param1/param2
in controller:
function myAction($arg1,$arg2)
{...}
In the view create link.
echo $this->Html->link('',array('controller'=>'Vehicles','action'=>'deleteimage',$param1,$param2),array('confirm'=>'Are you sure you want to delete the image?'));
In the above link I have sent two parameters to deleteimage function of the Vehicles controller.
In controller access the parameters by public function deleteimage($id, $image)
You can use named parameters, like this:
example.com/controller/action/param1:value/param2:value
In this canse you will find 'param1' and 'param2' in your controller in $this->passedArgs.
You can also define a custom route:
Router::connect('/news/:date/:article_name/:id',
array('controller'=>'articles', 'action'=>'view'),
array('pass' => array('id'), 'id'=>'[\d]+')
);
In this case, the action view in ArticlesController will be called with 'id' as the argument (and the route will only be matched if id passes the check for only containing digits). You can then also access 'date' and 'article_name' in the variable $this->params.