This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » support » General discussion » link_to backend (urgent, help!)
link_to backend (urgent, help!) [message #31104] Mon, 09 July 2007 16:58 Go to next message
microwaves  is currently offline microwaves
Messages: 16
Registered: March 2007
Junior Member
Hi,

I urgently need to solve this: how can i do a link to another app? For instance, a frontend link that points to the backend main page (sort of an 'Administration panel' link)?


Please help, i'm in a rush Confused
Re: link_to backend (urgent, help!) [message #31105 is a reply to message #31104 ] Mon, 09 July 2007 17:06 Go to previous messageGo to next message
nautilebleu  is currently offline nautilebleu
Messages: 281
Registered: June 2006
Location: Caen, France
Faithful Member

Here's an helper that allows links between apps:

/**
  * a function that allows link_to between apps
  * @param  $app    string      the app we want to go to
  * @param  $route  string      the route in the app. Must be valid
  * @param  $args   array       the arguments required by the route. Optional
  *
  */
function cross_app_link_to($app, $route, $args=null)
{
  /* get the host to build the absolute paths
     needed because this menu lets switch between sf apps
  */ 
  $host = sfContext::getInstance()->getRequest()->getHost() ;
  /* get the current environment. Needed to switch between the apps preserving
     the environment
  */
  $env = sfConfig::get('sf_environment');
  /* get the routing file 
  */
  $appRoutingFile = SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.$app.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'routing.yml' ;
  /* get the route in the routing file */
  /* first, substract the @ from the route name */
  $route = substr($route, 1, strlen($route)) ;
  if (file_exists($appRoutingFile))
  {
    $yml = sfYaml::load($appRoutingFile) ;
    $routeUrl = $yml[$route]['url'] ;
    if ($args)
    {
      foreach ($args as $k => $v)
      {
        $routeUrl = str_replace(':'.$k, $v, $routeUrl) ;
      }
    }
    if (strrpos($routeUrl, '*') == strlen($routeUrl)-1)
    {
      $routeUrl = substr($routeUrl, 0, strlen($routeUrl)-2) ;
    }
  }
  if ($env == 'dev')
  {
  	  $path = 'http://' . $host . '/' . $app . '_dev.php' . $routeUrl ;
  }
  else 
  {
  	$path = 'http://' . $host . $routeUrl ;
  }
  return $path ;
}



It's not very clean, but it works ! Cool

<edit>
Use :

<?php use_helper('Name_of_helper_where_you_put_the_code_above') ;  ?>
<?php echo link_to('Label', cross_app_link_to('backend', 'homepage')) ; ?>



</edit>

[Updated on: Mon, 09 July 2007 17:08]


all about me
t-shirts
icon14.gif  Re: link_to backend (urgent, help!) [message #31130 is a reply to message #31104 ] Mon, 09 July 2007 23:39 Go to previous messageGo to next message
microwaves  is currently offline microwaves
Messages: 16
Registered: March 2007
Junior Member
It's freaking great! Very well done Smile

Thanks A LOT, i wasn't going to have this going on time!
Re: link_to backend (urgent, help!) [message #31190 is a reply to message #31105 ] Tue, 10 July 2007 15:53 Go to previous messageGo to next message
Jackovson  is currently offline Jackovson
Messages: 83
Registered: March 2007
Member
Very interesting !

Maybe you could post it as a snippet ? (maybe allready done ? Laughing )
icon4.gif  Re: link_to backend (urgent, help!) [message #78360 is a reply to message #31104 ] Thu, 14 May 2009 17:11 Go to previous messageGo to next message
jorge.chavez  is currently offline jorge.chavez
Messages: 34
Registered: May 2009
Location: Monterrey, México
Member

I'm bran new at symfony and I'm about to finish the Jobeet tutorial,and I wannted to add a link from the frontend to the backend.
I tried the snipet, but I got an error on about the SF_ROOT_DIR constant.

"Notice: Use of undefined constant SF_ROOT_DIR - assumed 'SF_ROOT_DIR' in /home/jchavez/workspace/jobeet/lib/vendor/symfony/lib/helper /UrlHelper.php on line 602"

Has anyone used it?

I pasted it on the UrlHelper.php, is it ok to do that?

Is this constant defined in symfony 1.2?

index.php/fa/1126/0/

Re: link_to backend (urgent, help!) [message #78366 is a reply to message #78360 ] Thu, 14 May 2009 17:34 Go to previous messageGo to next message
Rubino  is currently offline Rubino
Messages: 104
Registered: April 2009
Location: Barcelona
Senior Member
You can change the constant in the helper to sfConfig::get('sf_root_dir') and it should fix the issue.


Virtual Machine for Symfony & Zend PHP Framworks
Website: http://www.sipx.ws
Support & Project tracker: http://trac.sipx.ws/projects/show/symfony-vm
Re: link_to backend (urgent, help!) [message #78375 is a reply to message #78366 ] Thu, 14 May 2009 18:46 Go to previous messageGo to next message
jorge.chavez  is currently offline jorge.chavez
Messages: 34
Registered: May 2009
Location: Monterrey, México
Member

Nice! Thanks a lot!

I am using the function and is working perfectly for my environments!

Very Happy

I did what you suggested and works fine!

  $env = sfConfig::get('sf_environment');
  
  $sf_root_dir = sfConfig::get('sf_root_dir');
  
  // Gets the routing file
  $appRoutingFile = $sf_root_dir.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.$app.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'routing.yml' ;
Re: link_to backend (urgent, help!) [message #78473 is a reply to message #31104 ] Fri, 15 May 2009 19:11 Go to previous messageGo to next message
jorge.chavez  is currently offline jorge.chavez
Messages: 34
Registered: May 2009
Location: Monterrey, México
Member

I'm having troubles using the helper I created: CrossAppUrlHelper, which I put it on /lib/helper dir. The problem seems to be that I cannot call use_helper('CrossAppUrl') on my frontend layout.

I'll try to explain myself better...

I'm doing the Jobeet Tutorial, and I'm trying to add a link from the frontend to the backend. Yesteday was working fine, but just because I pasted the function cross_app_link_to into the UrlHelper.php (yes, the symfony core url helper). Today I realized that if I upgrade to symfony 1.2.7, the change I made would display an error, in fact it did Sad

What I'm trying to do is to add these lines on the layout.

<?php use_helper('CrossAppUrl') ?>


And this on the footer:
<li class="backend">
              <?php // echo link_to(__('Become an affiliate'), cross_app_link_to('backend', '@homepage')) ?>
            </li>       


This is my layout:
<!-- apps/frontend/templates/layout.php -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>
      <?php if (!include_slot('title')): ?>
        Jobeet - Your best job board
      <?php endif; ?>
    </title>
    <link rel="shortcut icon" href="/favicon.ico" />
    <link rel="alternate" type="application/atom+xml" title="Latest Jobs"
      href="<?php echo url_for('@job?sf_format=atom', true) ?>" />
    <?php use_javascript('jquery-1.3.2.min.js') ?>
    <?php use_javascript('search.js')?>
    <?php include_javascripts() ?>
    <?php include_stylesheets() ?>
  </head>
  <body>
    <div id="container">
      <div id="header">
        <div class="content">
          <h1><a href="<?php echo url_for('@homepage') ?>">
            <img src="/images/logo.gif" alt="Jobeet Job Board" />
          </a></h1>
 
          <div id="sub_header">
            <div class="post">
              <h2>Ask for people</h2>
              <div>
                <a href="<?php echo url_for('@job_new') ?>">Post a Job</a>
              </div>
            </div>
 
            <div class="search">
              <h2>Ask for a job</h2>
              <form action="<?php echo url_for('@job_search') ?>" method="get">
                <input type="text" name="query" value="<?php echo $sf_request->getParameter('query') ?>" id="search_keywords" />
                <input type="submit" value="search" />
                <img id="loader" src="/images/ajax-loader.gif" style="vertical-align: middle; display: none" />
                <div class="help">
                  Enter some keywords (city, country, position, ...)
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
 
      <div id="content">
        <?php if ($sf_user->hasFlash('notice')): ?>
          <div class="flash_notice">
            <?php echo $sf_user->getFlash('notice') ?>
          </div>
        <?php endif; ?>
 
        <?php if ($sf_user->hasFlash('error')): ?>
          <div class="flash_error">
            <?php echo $sf_user->getFlash('error') ?>
          </div>
        <?php endif; ?>
        
        <div id="job_history">
          Recent viewed jobs:
          <ul>
            <?php foreach ($sf_user->getJobHistory() as $job): ?>
              <li>
                <?php echo link_to($job->getPosition().' - '.$job->getCompany(), 'job_show_user', $job) ?>
              </li>
            <?php endforeach; ?>
            </ul>
        </div>
 
        <div class="content">
          <?php echo $sf_content ?>
        </div>
      </div>
 
      <div id="footer">
        <div class="content">
          <span class="symfony">
            <img src="/images/jobeet-mini.png" />
            powered by <a href="http://www.symfony-project.org/">
            <img src="/images/symfony.gif" alt="symfony framework" /></a>
          </span>
          <ul>
            <li>
              <a href=""><?php echo __('About Jobeet') ?></a>
            </li>
            <li class="feed">
              <?php echo link_to(__('Full feed'), '@job?sf_format=atom') ?>
            </li>
            <li>
              <a href=""><?php echo __('Jobeet API') ?></a>
            </li>
            <li class="last">
              <?php echo link_to(__('Become an affiliate'), '@affiliate_new') ?>
            </li>
            <li class="backend">
              <?php // echo link_to(__('Become an affiliate'), cross_app_link_to('backend', '@homepage')) ?>
            </li>       
          </ul>
          <?php include_component('sfJobeetLanguage', 'language') ?>
        </div>
      </div>
    </div>
  </body>
</html>


And the error I get is:(Check the uploaded image to see the issue)

No matter where I put the use_helper('CrossAppUrl'), it says the function is undefined and displays the actual code of the function.
Has anyone tried to use use_helper in a layout? is it possible?

Re: link_to backend (urgent, help!) [message #78475 is a reply to message #78473 ] Fri, 15 May 2009 19:24 Go to previous message
Rubino  is currently offline Rubino
Messages: 104
Registered: April 2009
Location: Barcelona
Senior Member
The docco is your friend.
Scroll down to the section: "Adding Your Own Helpers" in http://www.symfony-project.org/book/1_2/07-Inside-the-View-L ayer

Remember that the file has to be named according to the helper format shown in the documentation.

[Updated on: Fri, 15 May 2009 19:25]


Virtual Machine for Symfony & Zend PHP Framworks
Website: http://www.sipx.ws
Support & Project tracker: http://trac.sipx.ws/projects/show/symfony-vm
Previous Topic:Question about symfony and browser event
Next Topic:Autoloading behaves differently in different environments
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software