在laravel路线上缺少斜线

时间:2018-04-22 14:46:45

标签: php laravel class routes

如果我使用此选项功能上传12345.jpg

Route::post('upload', function(){
      $options['upload_url'] = url('/images/');  
      $options['upload_dir'] = public_path().'/images/';
      $upload_handler = new App\Http\Controllers\UploadController($options);
 });

我收到错误网址

localhost:8000/public/images12345.jpg  miss the images/

如果我不使用上述功能

Route::post('upload', function(){
      //$options['upload_url'] = url('/images/');  
      //$options['upload_dir'] = public_path().'/images/';
      $upload_handler = new App\Http\Controllers\UploadController();
 });

我得到了正确的路线(作为控制器默认值)

localhost:8000/public/files/12345.jpg

UploadController.php

 public function __construct($options = null, $initialize = true, $error_messages = null) {
        $this->response = array();
        $this->options = array(
            'script_url' => $this->get_full_url().'/'.$this->basename($this->get_server_var('SCRIPT_NAME')),
            'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/files/',
            'upload_url' => $this->get_full_url().'/files/',

任何人都可以告诉我如何解决问题吗?

1 个答案:

答案 0 :(得分:0)

问题来自使用url()方法的UrlGenerator->to()方法。此方法调用trim($path.'/'.$tail, '/')以切断尾部斜杠(see code reference)。

因此,您可以使用url().'/images/代替url('/images/')

或者,作为更强大的替代方案

在控制器中添加一个检查,确保有一个尾部斜杠:

if (substr($this->options['upload_url'], -1) !== '/') {
    $this->options['upload_url'] .= '/';
}