TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Usage Examples (aka Snippet Share)
  • Using hard-coded links are not very true because when the Route have to rewrite all the references in the code. And the use of Route :: url very awkward and uncomfortable. So I wrote a wrapper for this method. To speed up the method use cache.

    /**
     * Create a cache tag(id) using the attributes methods.
     * 
     * @return  string
     */
    public static function tag()
    {
        return serialize(func_get_args());
    }
    
    /**
     * Create URL based on route. This is a shortcut for: Route::url. URL cached.
     * 
     *     CMS::url('route:default'); // Home page
     *     CMS::url(''); // Current URL
     *     CMS::url('route:current,action:delete,id:2'); // Based on current route
     *     CMS::url('controller:article,action:list,page:2'); // Based on default route
     *     CMS::url('route:articles,controller:article,action:list,page:2');
     * 
     * @param   string   $params       String with route params
     * @param   boolean  $escape_html  Convert url special characters to HTML entities?
     * @param   boolean  $lower_case   Convert url to lower case?
     * @param   mixed    $protocol     Protocol string or boolean, adds protocol and domain
     * @return  string
     * @uses    Request, Route, HTML, Text, UTF8
     * @uses    CMS::tag
     * @throws  Kohana_Exception
     */
    public static function url($params_str, $escape_html = TRUE, $lower_case = TRUE, $protocol = NULL)
    {
        if (empty($params_str))
        {
            // Get current page URL
            $url = Request::initial()->url($protocol);
        }
        else
        {
            // Try load URL from cache
            if (Kohana::$caching)
            {
                $tag = CMS::tag('url', $params_str, $escape_html, $lower_case, $protocol);
                
                if ($url = Kohana::cache($tag))
                {
                    return (string) $url;
                }
            }
            
            // Modify URL string in JSON and convert in array
            $url = str_replace(array(',', ':'), array('","', '":"'), $params_str);
            if ($params = json_decode('{"'.$url.'"}', TRUE, 2))
            {
                $route = isset($params['route']) ? $params['route'] : 'default';
                unset($params['route']);
                
                if ($route == 'current')
                {
                    $request = Request::current();
                    
                    $params = array_merge($request->param(), $params);
                    $params += array(
                        'directory'  => $request->directory(),
                        'controller' => $request->controller(),
                        'action'     => $request->action(),
                    );
                    
                    $route = Route::name($request->route());
                }
                
                $url = Route::url($route, $params, $protocol);
            }
            else
            {
                // Use links in format: 'option:value,option2:value2'
                throw new Kohana_Exception('Wrong url format: :url', array(':url' => $params_str));
            }
        }
        
        // Use URL modificators
        $url  = ($escape_html ? HTML::chars($url) : $url);
        $url  = ($lower_case ? UTF8::strtolower($url) : $url);
        $url  = str_replace('\\', '/', $url).'/';
        $url  = Text::reduce_slashes($url);
        
        // Save result URL in cache
        if ( ! empty($params_str) AND Kohana::$caching)
        {
            Kohana::cache($tag, $url, Date::DAY);
        }
        
        return $url;
    }
    
  • Most of the assets modules have the same problem: they didnt copy the associated files. For fix it i create assets contorller. My assets files are stored in DOCROOT/assets/, source - in APPPATH/media/.

    Route::set('assets', 'assets/<file>', array(
            'file' => '[\w\.\-\/]+'
        ))
        ->defaults(array(
            'directory'  => '',
            'controller' => 'Assets',
            'action'     => 'copy',
            'file'       => NULL,
        ));
    
    class Controller_Assets extends Controller
    {
        public function action_copy()
        {
            // Get the file path from the request
            $file = $this->request->param('file');
            extract(pathinfo($file), EXTR_SKIP);
            if ($filename = Kohana::find_file('media', $dirname.DIRECTORY_SEPARATOR.$filename, $extension))
            {
                $path = DOCROOT.'assets'.DIRECTORY_SEPARATOR.$dirname;
                if ( ! is_dir($path))
                {
                    // Create path in assets directory
                    mkdir($path, 0755, TRUE);
                }
                // Copy file in assets directory
                copy($filename, $path.DIRECTORY_SEPARATOR.$basename);
                
                // Send the file content as the response
                $this->response->body(file_get_contents($filename));
                // Set the proper headers to allow caching
                $this->response->headers('content-type',  File::mime_by_ext($extension));
                $this->response->headers('last-modified', date('r', filemtime($filename)));
            }
            else
            {
                // Return a 404 status
                $this->response->status(404);
            }
        }
    }
    
  • Need example with NAMESPACES in application/classes.

    I have folder Port and Port/Processor.

    In Port i'm have base abstract class Processor. In Port/Processor i'm have class Booking, that should extend from Processor.

    How i'm can do it work with Namespaces? How i need name my classes?

    I'm try namespaces like Port\Processor or just Port for Processor and different names, like Processor or Port_Processor but it's not work when i'm try use it in controller.

    use Port or use Port\Processor don't work.

  • @seyfer you should post a new thread with this question, you'd get better response. Include the version of kohana you are using. You shouldn't have to do anything special to usr namespaces, certainly for 3.3.x

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion