Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Controller Hybrid and template object
  • Hello. I have problem with implementing a Hybrid controller. I'm added folder via Ajax request, therefore I changed extendend controller_template on hybrid controller. When I used my Ajax request all looks fine, but I get error from before method: Attempt to assign property of non-object in C:\wamp\www\fuelphp\fuel\modules\filebrowser\classes\controller\admin.php on line 89 Code from 89 line:
    $this->template->config = $this->config; Could you help me to solve this problem?
  • Start by telling us what's in that before(), because I might be bad at guessing. But my guess is that you either don't call parent::before(), or you do it after that line, so $this->template doesn't contain the view object.
  • Hi. Before and Add folder method:
        public function before()
        {
    
            parent::before();
    
            Lang::load('filebrowser::global');
    
            if(!is_dir($this->image_thumbs))  {
                mkdir($this->image_thumbs, 0777);
            }
    
            $this->config = Config::load('upload');
    
    
            if($this->config['ext_whitelist'])
            {
                $this->swf_upload['file_types'] = "*." . implode(';*.', $this->config['ext_whitelist']);
            }
    
            $this->template->config = $this->config;
    
            $this->path = new Path(Input::get('path', $this->root_path));
    
            $data['mode'] =  Input::get('mode', 'none');
            $data['field'] =  Input::get('field', 'default');
            $data['type'] =  Input::get('type', 'all');
            $data['query'] =  $this->build_query($data);
            $data['uri'] = Uri::current() . $data['query'];
            $this->data = $data;
        }
    
    
        public function action_add_folder()
        {
    
    
            $directory = null;
            if(Input::post('name'))
            {
                $this->path = new Path(Input::post('path', Input::get('path', $this->config['path'])));
                $name = Inflector::friendly_title(Input::post('name'), '_');
                $directory = File::create_dir(Input::post('path'), $name);
                if($directory)
                {
                    $delete_tree = Cache::delete('filebrowser-directory-tree');
                    $delete_files = Cache::delete('filebrowser-files-' . $this->path->slug());
                }
                file_put_contents('add-dir.txt', 'Add folder:' . Input::post('name'). ' ', FILE_APPEND);
            }
            $response =  $this->response(array(
                'directory' => $directory,
            ));
    
            return $response;
            $this->template = null;
    
        }
    

    When I run add_folder action directly from browser return this json result: {"directory":null}. But when I run this action via AJAX then show me error: Error - 2012-10-12 09:30:17 --> 2 - Attempt to assign property of non-object in C:\wamp\www\fuelphp\fuel\modules\filebrowser\classes\controller\admin.php on line 90
    $( "#dialog-form-directory" ).dialog({
      autoOpen: false,
      resizable: false,
      draggable: false,
      height: 160,
      width: 360,
      modal: true,
      buttons: {
       "Create": function() {
        var bValid = true;
        bValid = bValid && checkLength( name, "nazwa", 3, 16 );
    
        if ( bValid ) {
          $.ajax({
            type: 'POST',
            url: "http://fuelphp/admin/filebrowser/add_folder",
            data: {path: pathq.val(), name: name.val()},
            success: function(data){
             location.reload(true);
            }
          });
        }
       },
       "Cancel": function() {
        $( this ).dialog( "close" );
       }
      },
      close: function() {
       tips.removeClass( "error3");
      }
     });
    
  • If you call the controller using AJAX, the Hybrid (and REST) controller assume you want to return data (in the form of xml or json). It therefore doesn't setup any views, that would be a wasting CPU cycles as you're not going to generate HTML. So either don't use $this->template when you're using ajax calls, or use \Input::is_ajax() to see if it's an ajax call before attempting to use $this->template.

Howdy, Stranger!

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

In this Discussion