Doubt Laravel 5 - Libraries and Functions

2
 <?php namespace App\Http\Controllers;

 use App\Http\Requests;
    use App\Http\Controllers\Controller;
    use App\ListaProduto;
    use App\Produto;
    use Input;
    use Illuminate\Support\Facades\Request;
    use Session;

    class ProdutoController extends Controller {

        # Página de Exibir Produtos
        public function getAdicionar(){
            return view('backend.produtos');
        }
    }

I was recently developing websites and systems using Laravel 4 . And now I'm migrating to Laravel 5 .

I'm noticing that some functions that existed before in 4, are no longer compiled in 5. An example:

The Facade / HTML library.

I'm having to install this library on every site that I create the project on Laravel 5 . Beauty, that's the least. Type in the composer.json file and run it.

However, something happens to me that I do not know if it's normal, if it's native to Laravel 5 or if it's just a command I need to do so it does not happen anymore. In the code I posted above see the first few lines:

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\ListaProduto;
use App\Produto;
use Input;
use Illuminate\Support\Facades\Request;
use Session;

Every time I'm going to use some function I need to declare. Before, in 4, with me, it was not like that. Even a Model I need to tell the code I'm going to use. If I want to use the Product table, I have to describe use App\Produto; and so on with Session , with Cookies , with Hash .

Questions

1 - Is this really necessary? Home 2 - Is there anything I can do to reverse this?

    
asked by anonymous 13.08.2015 / 18:29

1 answer

1

Likely, the Laravel staff, when developing the 5 version, checked for possible problems with the class name conflict and then decided to adopt namespace so that this would not happen.

Explanation

In Laravel 4, for example, I could not have a table named events in my application, because if I created the model Event , because that class already exists in Laravel 4 , it would generate an error. p>

  

Can not redeclare class Event

Is this really necessary?

In my opinion, if there is a possible name collision problem, you should avoid it. But overall, if you think you can keep this under control, you can install the Illuminate\Facade packages via composer and configure them in the same way as Laravel 4.

Here is a small example:

link

Is there anything I can do to reverse this?

Perhaps one of the ways to solve this would be to use BaseController and instantiating some things that will be reused throughout the project.

Theoretical example:

use Illuminate\AlgumPacote\Input;

class BaseController extends Controller
{
   public function __construct()
   { 

      $this->input = new Input();
   }

   public function input()
   {
       return $this->input;
   }
}



class ProdutoController extends BaseController
{
  public function getIndex()
  {
     $this->input()->get('teste');
  }
}
    
14.08.2015 / 22:41