0

I'm using CakePHP 3.x+.

I have created a library class to download youtube videos using youtube-dl and return local URL of the file.

APP/src/Library/AnujTools/YoutubeDownload.php

<?php
namespace App\Library\AnujTools;

use Cake\Routing\Router;

class YoutubeDownload
{
    public function download()
    {
        $user_upload_dir = 'files/youtube-video/123/';
        $file_name = '324354554.mp4'

        //
        // download script here
        //

        $url = Router::url('/', true) . $user_upload_dir . $file_name;
        return $url;
    }
}

The file is downloaded to location

WWW_ROOT . 'files/youtube-video/123/324354554.mp4';

and return statement must return

https://example.com/files/youtube-video/123/324354554.mp4

But it returns only

files/youtube-video/123/324354554.mp4

Debugging debug(Router::url('/', true)); gives only /

Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
  • You can use array path like `[controller => Pages,action=>index]` and please show your `routes.php` file – Pradeep Singh Feb 06 '18 at 10:26
  • I want the base URL, not URL of the index page, only the domain name. – Anuj TBE Feb 06 '18 at 10:28
  • `$this->Url->build('/', true);` might help – Pradeep Singh Feb 06 '18 at 10:36
  • The `App.fullBaseUrl` config option is probably empty, for whatever reason (`bootstrap.php` should set it if not already defined)... – ndm Feb 06 '18 at 11:02
  • setting `App.fullBaseUrl` is not working though. `Router::url('/', true);` works in Controller and model. But, I want to use it in my Library class file. – Anuj TBE Feb 06 '18 at 12:03
  • "_doesn't work_" isn't an overly helpful depiction of the situation I'm afraid, as that config option is the source where the router gets the base URL from (unless manually set via `Router::fullBaseUrl()`). You'll have to do some more debugging, and/or provide more context, starting with where exactly and how exactly your class is being used. – ndm Feb 06 '18 at 14:37

0 Answers0