I have followed the cakephp documentation for 2.0 to create a restFUL. I am not sure if I have it right.
If I was to just put the URL into the browser should I see the xml called back. I am just trying to test it but all I see is the standard view and not the xml view. I just want a quick test to see if I have it right.
The URL
http://www.mydomain.com/members/123.xml
The controller is Members and the method I am calling is view
Here is my code:
routes.php
Router::mapResources('members');
Router::parseExtensions('xml', 'json');
MembersController.php
public function view($id = null) {
if (!$this->Member->exists($id)) {
throw new NotFoundException(__('Invalid member'));
}
$options = array('conditions' => array('Member.' . $this->Member->primaryKey => $id));
$members = $this->Member->find('first', $options);
$this->set(array(
'member' => $members,
'_serialize' => array('member')
));
}
app/view/members/xml/view.ctp
echo $xml->serialize($member)
Have you the RequestHandler in your components array? If not put it in there.
See this page in the CakePHP book.
You don't need any view, CakePHP handles it automatically. Delete folder app/view/members/ with all files inside.
Related
I am working on CakePHP and I have a URL http://admin.example.com/Pages .
How can I create http://admin.example.com/Pages.html ? Is there any solution or component to solve this issue?
According to CakeBook , You can define extensions you want to parse in routes
E.g. Write the code below in app/Config/routes.php
Router::parseExtensions('html');
This will allow you to send .html extenstion in routes(url)
Not create a link
E.g:
$this->Html->link('Link title', array(
'controller' => 'pages',
'action' => 'index',
'ext' => 'html'
));
When you click that link you will get url in browser something like this
http://admin.example.com/pages/index.html
Do you really need this? CakePHP is automatically render view files from View folder
You can create (if its PagesController and index methond) index.ctp in app/View/Pages folder and it will be automatically render.
You can use file_get_contents function to read files. Something like this:
// in controller
function index() {
$content = file_get_contents('path/to/file.html');
echo $content;
die();
}
I am looking for input/help on how to do this. Might be some PHP/cake developers could provide some good solutions here. Cakephp 2.3 something :)
Problem; How to put shortcodes in wysiwyg editor (example: [slideshow=1]slideshow here[/slideshow]) and render an element (in this case, loading and displaying the slideshow with ID=1).
ShortcodeHelper.php
App::import('Helper', 'Html', 'Router');
class ShortcodeHelper extends AppHelper {
public $shortcodes = array(
'slideshow' => '/(\[slideshow=)(.+?)(\])(.+?)(\[\/slideshow\])/'
);
public $returncodes = array(
//'slideshow' => $this->render('/elements/slideshow', array('id'=>'\\2'))
'slideshow' => '<strong rel="\\2">\\4</strong>'
);
public function render($content, $render=null) {
$shortcodes = $this->shortcodes;
$returncodes = $this->returncodes;
if(isset($render)) {
$temp_shortcodes = array();
$temp_returncodes = array();
foreach ($render as $key => $value) {
$temp_shortcodes[$key] = $shortcodes[$value];
$temp_returncodes[$key] = $returncodes[$value];
}
$returncodes = $temp_returncodes;
$shortcodes = $temp_shortcodes;
}
$return = preg_replace($shortcodes, $returncodes, $content);
return $this->output($return);
}
}
view.ctp (call render function from helper, and run the page-content trough it):
<?php echo $this->Shortcode->render($page['Page']['body']); ?>
Thanks. You are awesome!! :)
-Tom
You need to turn the short code string into a method call, parse it.
Your helper will need to be able to detect them and then break them up. Your code needs to be mapped somehow to a callback.
// [slideshow=1]slideshow here[/slideshow]
$this->requestAction(array('controller' => 'slideshows', 'action' => 'view', $id);
For example.
I think the best way here would be to just always map the first arg, the "function call" to an element instead and pass all other args to the element. This way you can do there whatever you want and request the data or just simply display HTML only.
I would put the mapping of short codes into something like Configure::write('ShortCodes', $shortCodeArray); this way plugins could even register their callback mapping by simply adding them to that array.
array(
'slideshow' => array('controller' => 'slideshows', 'action' => 'view')
);
You'll have to merge that with args from the parsed short code.
Why requestAction()? You should not violate the MVC pattern, for this reason you'll have to request the data via requestAction().
I have problem with cakephp and amfphp 2.1 integration.
I made following controller:
class AmfController extends AppController
{
public function index(){
App::import('Vendor','Amfphp/index');
$this->autoRender = false;
}
public function backOffice(){
App::import('Vendor', 'backOffice', array('file' => 'BackOffice' . DS . 'ServiceBrowser.php'));
$this->autoRender = false;
}
}
Method index is working perfectly, cakephp is outputting amf entry point, but method backOffice is outputting following error:
Service call failed
object(CakeRequest) {
params => array(
[maximum depth reached]
)
data => array([maximum depth reached])
query => array([maximum depth reached])
url => 'amf/backOffice'
base => ''
webroot => '/'
here => '/amf/backOffice'
}
object(CakeResponse) {
}
Please help me, folder Amfphp and BackOffice are located in app/Vendor folder.
Here's a thought that might help: the service browser calls the entry point to get information about the various services, so that might create some bizarre side effects in CakePHP resulting in an infinite loop.
Other than that I don't know... If you still have trouble post back and I will give it a try.
I am developing (with a partner) a CakePHP application which will use Backbone.js.
So, basically, my cake application largely behaves like a JSON API. After the controller action loads a certain view (no data is rendered yet), Backbone makes an AJAX call to another controller action to fetch the data. The controller return JSON.
(The routes.php file has the mapResources and parseExtensions line in place)
Controller code
public function index(){
$company_id = $this->Session->read('Company.id');
$channel_ids = $this->Order->Channel->find('all', array('conditions' => array('Channel.company_id' => $company_id), 'fields' => array('Channel.id')));
$channel_ids = Set::extract('/Channel/id', $channel_ids);
$data = $this->paginate('Order', array('Order.fulfillment_status_id' => 1, 'Order.channel_id' => $channel_ids));
$this->set('orders', $data);
//In case of a JSON request
if($this->RequestHandler->ext == 'json') {
$this->autoRender = false;
echo json_encode($data);
}
}
This worked fine, but we hit a wall when we tried to implement pagination with this.
How do we generate the Pagination links? (If we do it via Backbone, it does not know the total records in the database and hence does not know how many links to generate)
How should the "page number" be burnt into the AJAX call? How should it be deciphered in the controller?
Really stuck here, we will really appreciate your help.
In Your Controller
$this->layout = false;
$this->RequestHandler->respondAs('json');
$recipes = $this->paginate('Recipe');
$this->set(compact('recipes'));
In your View
echo json_encode($recipes);
echo json_encode($this->Paginator->params());
I'm trying to build an admin page using the pages controller and a admin_index() function. I need to get a list of all Posts with a certain status and display them on this page. How can I grab these in the pages controller so I can then display them in the view.
Many thanks in advance.
try this :
$this->loadmodel('Post');
$posts = $this->Post->find('all',array('conditions'=>array('Post.id'=>'1','Post.field'=>'value')));
$this->set('posts',$posts);
You can load the model in your PagesController's admin_index() function:
$this->loadModel('Post');
$posts = $this->Post->find('all', array(
'conditions' => array('Post.status' => 'your_filter')
);
$this->set(compact('posts'));
Now you have $posts available in your pages' view file. (Adjust find method to your needs)