Doubt Laravel 5 - Libraries and Functions

Asked

Viewed 357 times

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');
        }
    }

It was until recently developing sites and systems using the Laravel 4. And now I’m migrating to the Laravel 5.

I am realizing that some functions previously existing in 4 are no longer compiled in 5. An example:

The library Facade/HTML.

I’m having to install this library on every website I design on Laravel 5. Okay, that’s the least of it. Type in the file Composer.json and have it rotated.

But something happens to me that I don’t know if it’s normal, if it’s native to Laravel 5 or if it’s just a command I need to make so it doesn’t happen anymore. In the code I posted above see the first 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 use some function I need to declare. Before, in 4, with me, it was not like this. Even one Model need tell for the code I will use you. If I want to use the table Product, I have to describe use App\Produto; and so on with Session, with Cookies, with Hash.

Questions

1 - This is really necessary ?
2 - There is something I can do to reverse this ?

  • +1. This question is very important for those who are willing to migrate from Laravel 4 to 5

1 answer

1


Probably the people from Laravel, when developing the version 5, verified possible problems with the conflict of class names and then decided to adopt the namespace so that this would not occur.

Explanation

In Laravel 4, for example, I could not have a table called events in my application, because if I created the model Event, by that class already exists in the Laravel 4, would create a mistake.

Cannot redeclare class Event

This is really necessary ?

The way I see it, if there’s 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 via Build and configure them the same way as Laravel 4.

Here is a small example:

https://www.flynsarmy.com/2015/02/install-illuminatehtml-laravel-5/

There’s something I can do to reverse this ?

Perhaps one of the ways to solve this would be by using the 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');
  }
}
  • Answered in parts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.