Google Groups Home
Help | Sign in
Alias?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Davide  
View profile
 More options Apr 18, 3:44 am
From: "Davide" <d...@linux.it>
Date: Fri, 18 Apr 2008 09:44:22 +0200 (CEST)
Local: Fri, Apr 18 2008 3:44 am
Subject: Alias?
Good morning,

I have a ProjectsController that works fine with the usual
add/edit/view/index.

I can successfully view a project with /projects/view/$id.

Each project has also a unique name, and I would like to refer to each
project even with a url like /projects/$name.

I thought about extending the AppError on the missingAction and do the
required operations (code in the "footer" of the mail).

requestAction retrieve successfully the project detail with the right
thtml but no layout is rendered.

How can I render the correct page without doing a redirect?

Another question: is it possible to specify a different ErrorHandler
for a controller? So it could be possible to avoid ugly
switch. Something like setting the errorHandler variable inside the
controller.

Thanks a lot
Davide

<?php
/*
 * app/error.php
 */
class AppError extends ErrorHandler{
   function missingAction($params){
      switch($params["className"]){
         case "ProjectsController":
            loadModel("Project");
            $model = new Project();
            $data = $model->findByName($params["action"]);
            echo
$this->requestAction("/projects/view/".$data["Project"]["id"],
array("return"));
            break;
         default:
            parent::missingAction($params);
            break;
      }

   }

}

?>

--
Live life like you're gonna die. Because you're gonna.
                                            William Shatner


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Davide  
View profile
 More options Apr 18, 4:40 am
From: "Davide" <d...@linux.it>
Date: Fri, 18 Apr 2008 10:40:16 +0200 (CEST)
Local: Fri, Apr 18 2008 4:40 am
Subject: Re: Alias?

Davide wrote:
> ...
> How can I render the correct page without doing a redirect?

At the moment I've solved in this way. Don't know if it's the right
way but it's working.

Thanks a lot
Bye
Davide

<?php
/*
 * app/error.php
 */
class AppError extends ErrorHandler{
   function missingAction($params){
      switch($params["className"]){
         case "ProjectsController":
            loadModel("Project");
            $model = new Project();
            $data = $model->findByName($params["action"]);
            if(is_null($data) || empty($data)){
               parent::missingAction($params);
            }else{
               $this->controller->webroot = $params["webroot"];
               $this->controller->set("content_for_layout",$this->requestAction("/projects /view/".$data["Project"]["id"],
array("return")));
               $this->controller->pageTitle = $data["Project"]["name"];
               $this->controller->viewPath = "layouts";
               $this->controller->render("ajax");
            }
            break;
         default:
            parent::missingAction($params);
            break;
      }

   }

}

?>

--
Live life like you're gonna die. Because you're gonna.
                                            William Shatner


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Davide  
View profile
 More options Apr 28, 10:31 am
From: "Davide" <d...@linux.it>
Date: Mon, 28 Apr 2008 16:31:25 +0200 (CEST)
Local: Mon, Apr 28 2008 10:31 am
Subject: Re: Alias?
I wrote a code (at the bottom) that in case of missingAction will search
in the database and if found then render the proper view.

Everything works fine except when I turn DEBUG=0 (production setup). Now
cake always render the 404 without entering the AppError::missingAction().

How can I do?

Following the code.

Bye and thanks
Davide

/*
 * app/error.php
 */
class AppError extends ErrorHandler{
   function missingAction($params){
      switch($params["className"]){
         case "ProjectsController":
            loadModel("Project");
            $model = new Project();
            $data = $model->findByAlias($params["action"]);
            if(is_null($data) || empty($data)){
               parent::missingAction($params);
            }else{
               $this->controller->webroot = $params["webroot"];
               $this->controller->set("content_for_layout",$this->requestAction("/projects /view/".$data["Project"]["id"],
array("return")));
               $this->controller->pageTitle = $data["Project"]["name"];
               $this->controller->viewPath = "layouts";
               $this->controller->render("ajax");
            }
            break;
         default:
            parent::missingAction($params);
            break;
      }

   }

}

--
Live life like you're gonna die. Because you're gonna.
                                            William Shatner

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joel Perras  
View profile
 More options Apr 28, 12:41 pm
From: Joel Perras <joelp...@gmail.com>
Date: Mon, 28 Apr 2008 09:41:24 -0700 (PDT)
Local: Mon, Apr 28 2008 12:41 pm
Subject: Re: Alias?
How about in one line?
In the app/config/routes.php file:

Router::connect('/projects/:id', array('action' => 'view'), array('id'
=> '[0-9]+'));

-J.

On Apr 28, 10:31 am, "Davide" <d...@linux.it> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joel Perras  
View profile
 More options Apr 28, 12:46 pm
From: Joel Perras <joelp...@gmail.com>
Date: Mon, 28 Apr 2008 09:46:49 -0700 (PDT)
Local: Mon, Apr 28 2008 12:46 pm
Subject: Re: Alias?
Whoops, didn't see that you wanted to also refer to the unique name of
the project.  So A few more lines.

For that, use:
(app/config/routes.php)
Router::connect('/projects/:name', array('action'=>'view',
'name'=>null));

And in your projects/view controller/action pair:
function view() {
    if (empty($this->params['name'])) {
       return $this->setAction('index');
    }

    $conditions = array('name'=>$this->params['name']);
    $project = $this->Project->find($conditions);
    $this->set(compact('project'));

}

-J.

On Apr 28, 12:41 pm, Joel Perras <joelp...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Davide  
View profile
 More options Apr 29, 6:52 am
From: "Davide" <d...@linux.it>
Date: Tue, 29 Apr 2008 12:52:11 +0200 (CEST)
Local: Tues, Apr 29 2008 6:52 am
Subject: Re: Alias?

Joel Perras wrote:
> ...
> For that, use:
> (app/config/routes.php)
> Router::connect('/projects/:name', array('action'=>'view',
> 'name'=>null));
> ...

Thanks Joel,

unfortunly Router::connect() seems not to work for me. Cake
1.1.18. It's simply ignored. But you gave me a hint and using
$Route->connect(...) I can achieve my goal.

However I have to add a $Route->connect() for each action and it's not
beautiful and subject to errors. I'm thinking in a workaround
solution. I will post it here.

Thanks for the hint.
Davide

--
Live life like you're gonna die. Because you're gonna.
                                            William Shatner


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Davide  
View profile
 More options May 5, 4:59 am
From: "Davide" <d...@linux.it>
Date: Mon, 5 May 2008 10:59:20 +0200 (CEST)
Local: Mon, May 5 2008 4:59 am
Subject: Re: Alias?

Davide wrote:
> unfortunly Router::connect() seems not to work for me. Cake
> 1.1.18. It's simply ignored. But you gave me a hint and using
> $Route->connect(...) I can achieve my goal.

> However I have to add a $Route->connect() for each action and it's not
> beautiful and subject to errors. I'm thinking in a workaround
> solution. I will post it here.

Worked a bit on it, but it took too much time for achieving a good
solution so I finally opted for mapping each action in routes.php and
managing the alias in the view action. So finally here is the code[1].

Now I'm asking myself about the utility of AppError if it's disabled
in a production environment. Can someone answer me maybe with a
scenario?

Thanks a lot.
Bye
Davide

1.

//routes.php
...
$Route->connect("/projects/",
array("controller"=>"projects","action"=>"index"));
$Route->connect("/projects/add/",
array("controller"=>"projects","action"=>"add"));
$Route->connect("/projects/delete/",
array("controller"=>"projects","action"=>"delete"));
$Route->connect("...
...
$Route->connect("/projects/view/",
array("controller"=>"projects","action"=>"view"));
$Route->connect("/projects/:alias",
array("controller"=>"projects","action"=>"view"));

//app/controllers/projects_controller.php
...
   function view($id=null){
      $data = null;
      if(empty($this->params["alias"]) && is_null($id)){
         $this->cakeError("error404",array($this->params["url"]));
      }else{
         if(!is_null($id)){
            $data = $this->Project->findById($id);
         }else{
            $data = $this->Project->findByAlias($this->params["alias"]);
         }
...

--
Live life like you're gonna die. Because you're gonna.
                                            William Shatner


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google