views:

71

answers:

5

Hello

I was trying to create an admin for my controller Posts, I used cake console option to create controller, I type Y for the question "Need admin routing ?", I entered Y and I got written my controller with admin functions, one of my admin function looks like this

function admin/controller_view($id = null) {
    if (!$id) {
        $this->Session->setFlash(__('Invalid post', true));
        $this->redirect(array('action' => 'index'));
    }
    $this->set('post', $this->Post->read(null, $id));
}

But when I execute I am getting the following error

Parse error: syntax error, unexpected '/', expecting '(' in C:\xampp\htdocs\cake\blog\app\controllers\posts_controller.php on line 61

I am using cakephp 1.3, What should I do to create an admin panel for this controller ?

Thaks a lot

+2  A: 

The slash / isn't a valid character to be used in the function name eg admin/controller. That is why the error is there. You should remove the slash and re-name the function as per CakePHP's function naming convention.

Sarfraz
thanks for the feedback, "admin/controller" created by the console automatically, that is not my code, I think I have to change some config in cakephp, any idea ?
Thomas John
@Thomas John: You can read the docs of CakePHP and rename the function accordingly :)
Sarfraz
A: 

in your routes file inside config folder, you can use something like this

Router::connect('/admin', array('controller' => 'admins', 'action' => 'index'));

And your admin controller should be something like this

class AdminsController extends AppController 
{
    var $name = 'Admins';
    var $components = array('Email','Session','RequestHandler','Cookie');
    var $uses = array('Admins', 'Setting', 'Movie', 'User', 'Invitation', 'Purchase','Package');


    function admin_index() 
    {
        $this->layout = 'admin_default';
    }
}

now you can open your admin : mysite.com/admin/admins.

You don't have to use "/" while defining function. Hope this help

nik
The cake console created / symbol on my PostsController, Should I remove those ?
Thomas John
@nik that has nothing to do with what the problem is
dogmatic69
A: 

what have you set up in core.php as the admin routing.. seems like you have 'admin/' it should just be 'admin'

dogmatic69
Thanks, I fixed it,now I am getting the /posts/ but I cant got to admin/posts ??
Thomas John
you should update your code example according to what you have so we can know what is going on
dogmatic69
A: 

Based on creating admin routing myself in cake, if you're trying to access the admin routing by going to mysite.com/admin/posts, then the function should have appeared as admin_view(), not admin/controller_view(). Unfortunately, I don't know what would have caused this, but correcting that should allow you to access the admin panel by going to mysite.com/admin/posts/view. If that doesn't work, can you post the contents of your routes.php?

LucasHammer
A: 

Thomas, you probably misunderstood something in cake's docs. Here's how it works:

If you ask for /posts/view cake looks for PostsController::view().

If you have admin routing switched on (i.e. "prefix routing"), and ask for /admin/posts/view, cake will look for PostsController::admin_view().

Notice it's in the same controller!

You will also need an additional view file admin_view.ctp of course.

It can be a bit confusing at first, but don't worry, you'll get there! ;)

dr Hannibal Lecter