0
votes

have read that to use/intercept URLs in drupal, a module can use hook menu to do the relevant processing.

Have tried to implement it according to the needs after seeing the tutorials on Drupal. however it doe not seem to working at all. when the url is entered, it simply returns the error that the page cannot be found.

the info file for module is as follows:

name = news service
description  = Module for news rest service
dependencies[] = services
files[] = news.module
core = 7.x

the .module file is as follows:

<?php

 function news_service_menu()
 {
     $items['/get/news'] = array(
        'page callback' => 'get_latest_news'
     );
     return $items;
 }

 function get_latest_news($page)
 {
     return "requested page is $page";
 }
/* drupal recommends not to add the ending php tag*/ 

drupal is installed at localhost/drupal . the url am trying is : localhost/drupal/get/news/1

to sum up the requirement, am trying to make a REST service that simply return a custom content type (news article) as a JSON .

any help will be appreciated.

1

1 Answers

4
votes

You shouldn't add a leading slash when registering a path. Change $items['/get/news'] to $items['get/news']

You may also need to specify access arguments and set permissions to certain roles, e.g.

'access arguments' => array('access custom pages'),

or use your own access callback function.

Read more about the hook_menu and access arguments here: https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_menu/7

When down-voting a question or answer please state your reasons so that others benefit from it.