I'm migrating from Laravel 4 to Laravel 5.
Only following is working:
Route::get('/some-url', function(){
echo "Some text";
});
Following is not working:
Route::get('/get-customer-info', 'CustomerController#info');
I created the controller in: app\Http\Controllers\CustomerController.php
So, whenever I redirect to a controller, it always set object not found
Here is my controller:
<?php
namespace App\Http\Controllers;
class CustomerController extends Controller
{
function __construct()
{
View::share('root', URL::to('/'));
}
public function info()
{
echo "ok";
}
}
I moved index.php file to the root.
Here is the index.php file on the
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* #package Laravel
* #author Taylor Otwell <taylorotwell#gmail.com>
*/
/*
|--------------------------------------------------------------------------
|Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels nice to relax.
|
*/
require __DIR__.'/bootstrap/autoload.php';
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
$app = require_once __DIR__.'/bootstrap/app.php';
/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
Related
i have url like this mysite.com/company, i want add prefix to url to become mysite.com/home/company.
I've tried to add a route group, but that requires me to update all existing routes.
can i add a prefix in all url without affecting the existing route ?
i used laravel 5.6
I have created a sandbox so that you can view and play around with the code used for this answer.
I know the sandbox uses a different Laravel version (version 7), but looking at the documentation for version 5.6 the routing does not seem to be that much different than that of version 7.
What you can do is wrap the already existing routes inside an anonymous function and assign it to a variable, you can then use this variable and pass it as a parameter to the group routing function along with a prefix, e.g.
$routes = function() {
Route::get('company', function () {
return 'companies';
});
Route::get('company/{company}', function ($company) {
return "company $company";
});
Route::delete('company/{company}', function ($company) {
return "deleting company $company...";
});
Route::get('company/{company}/staff', function ($company) {
return "staff list for company $company...";
});
};
Route::prefix('/')->group($routes);
Route::prefix('/home')->group($routes);
When running php artisan route:list the following is returned:
+--------+----------+------------------------------+------+---------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+------------------------------+------+---------+------------+
| | GET|HEAD | api/user | | Closure | api |
| | | | | | auth:api |
| | GET|HEAD | company | | Closure | web |
| | GET|HEAD | company/{company} | | Closure | web |
| | DELETE | company/{company} | | Closure | web |
| | GET|HEAD | company/{company}/staff | | Closure | web |
| | GET|HEAD | home/company | | Closure | web |
| | GET|HEAD | home/company/{company} | | Closure | web |
| | DELETE | home/company/{company} | | Closure | web |
| | GET|HEAD | home/company/{company}/staff | | Closure | web |
+--------+----------+------------------------------+------+---------+------------+
You can see above that the routes can now be accessed via both / and home/, e.g. http://example.com/company and http://example.com/home/company without the need for duplicating routes.
If you need to add any more prefixes in the future you just simply add a new Route::prefix("<prefix>")->group($routes); to the routes file.
The update below is in response to the comment provided by OP.
To get this correct you are looking for a way to automatically convert all instances of the url function from url('someurl') to url('home/someurl'), e.g.
url('company') will become url('home/company')
url('knowledgebase') will become url('home/knowledgebase')
If so then I have two solutions for you:
Can you not simply do a search and replace within the IDE you are using?
You can override Laravel's url helper function to prefix all path's with home/, to do so you can do the following:
I have created another sandbox so that you can view and play around with the code used for this answer.
First create a helpers.php file anywhere within your Laravel application, when testing this code I placed the file in the root directory of the application, e.g. /var/www/html/helpers.php.
Second you need to override the url helper function, to make sure you don't lose any functionality I grabbed the original source code of the function from Laravel's github repository. I then modified it to include the prefix, so place the following in the new helpers.php file:
<?php
use Illuminate\Contracts\Routing\UrlGenerator;
if (! function_exists('url')) {
/**
* Generate a url for the application.
*
* #param string|null $path
* #param mixed $parameters
* #param bool|null $secure
* #return \Illuminate\Contracts\Routing\UrlGenerator|string
*/
function url($path = null, $parameters = [], $secure = null)
{
if (is_null($path)) {
return app(UrlGenerator::class);
}
$path = "home/$path";
return app(UrlGenerator::class)->to($path, $parameters, $secure);
}
}
Next, you need to load your helpers.php file before Laravel loads their helper functions, otherwise it wont load, to do so add require __DIR__.'/../helpers.php'; to public/index.php before require __DIR__.'/../vendor/autoload.php';, e.g.
require __DIR__.'/../helpers.php';
require __DIR__.'/../vendor/autoload.php';
Now you can use the new url function within your application, I have given some examples in the web.php routes file:
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
echo url("company");
echo "<br/>";
echo url("knowledgebase");
});
The above will output to the webpage:
http://example.com/home/company
http://example.com/home/knowledgebase
Now if you need to change the url function slightly to more fit your requirements you can do so by modifying it within the helpers.php file.
I get an error message, that no route is found for "GET/users":
Level: Error, Channel: request, Message: Uncaught PHP Exception
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No
route found for "GET /users"" at
/Users/work/project/vendor/symfony/http-kernel/EventListener/RouterListener.php
line 139
But I actually set a route for "users", so I do not know how to solve this:
<?php
namespace App\Controller;
use DataTables\DataTablesInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
/**
*
* #Route("/users", name="users")
*
* #param Request $request
* #param DataTablesInterface $datatables
* #return JsonResponse
*/
class DataTableController extends Controller
{
const ID = 'users';
public function usersAction(Request $request, DataTablesInterface $datatables): JsonResponse
{
try {
// Tell the DataTables service to process the request,
// specifying ID of the required handler.
$results = $datatables->handle($request, 'users');
return $this->json($results);
}
catch (HttpException $e) {
// In fact the line below returns 400 HTTP status code.
// The message contains the error description.
return $this->json($e->getMessage(), $e->getStatusCode());
}
}
}
The annotation on top of the class only defines a prefix, not the actual route. The reason why becomes clear when you have multiple methods in the class. How can the router identify which one to call?
In other words you either have to move the annotation to the method or add another one like this to the action:
<?php declare(strict_types = 1);
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* #Route("/users", name="users_")
*/
class UserController
{
/**
* #Route("", name="list")
*/
public function index()
{
return new Response('TODO: Add user list');
}
}
This will append the route definition from the action to the one on the class, like one would expect from a prefixed route.
If you want to know which routes Symfony recognizes you can use the debug command in the console:
php bin/console debug:router
or for a specific route:
php bin/console debug:router users
This is the output I get in my demo application:
php bin/console debug:router
------------ -------- -------- ------ --------
Name Method Scheme Host Path
------------ -------- -------- ------ --------
users_list ANY ANY ANY /users
------------ -------- -------- ------ --------
php bin/console debug:router users_list
+--------------+---------------------------------------------------------+
| Property | Value |
+--------------+---------------------------------------------------------+
| Route Name | users_list |
| Path | /users |
| Path Regex | #^/users$#sD |
| Host | ANY |
| Host Regex | |
| Scheme | ANY |
| Method | ANY |
| Requirements | NO CUSTOM |
| Class | Symfony\Component\Routing\Route |
| Defaults | _controller: App\Controller\UserController::index |
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
+--------------+---------------------------------------------------------+
If you want to find out which URL-path matches to which route you can also use the following command:
php bin/console router:match "/users"
Optionally you can specify additional parameters like the method using CLI-option:
php bin/console router:match "/users" --method="POST" --scheme="https" --host="example.com"
I have a middleware class :-
<?php
namespace App\Http\Middleware;
use Closure;
class isAdmin
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if(auth()->user()->getIsAdminAttribute()){
return $next($request);
}
abort(404);
}
}
In Kernel.php file:-
<?php
namespace App\Http;
use App\Http\Middleware\isAdmin;
protected $routeMiddleware = [
'isAdmin' => isAdmin::class,
];
In web.php route file:-
Route::get('users/list', 'UsersController#listUsers')->name('List_Users')->middleware('isAdmin');
I've tried to dump the
dd(auth()->user()->getIsAdminAttribute());
Nothing happen , like I am not assigning isAdmin middleware to that route at all.
You could check your routes and middleware assigned to them with this command :
php artisan route:list
Should look like :
+--------+----------+-------------+------------+-------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-------------+------------+-------------------------------------------------+--------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | users/list | List_Users | App\Http\Controllers\UsersController#listUsers | web,isAdmin |
+--------+----------+-------------+------------+-------------------------------------------------+--------------+
Note the isAdmin in the middleware column.
i know it's late for you but maybe someone else will benefit from it,
i had the same issue, and after some debugging i found that the problem the middlware wasn't fired and appear in route:list due to the Route::get('post/{id}') even if i put this route after the route who was facing the issue Route::get('post/create'), i changed the show route from post to posts and the middleware get fired again.
I'm trying to do dependency injection in Laravel to keep my controllers and models as slim as possible. The goal is to have repositories to handle the fetching of data attributed to certain models.
To this end I'm trying to follow the example from the documentation here and a popular Laravel boilerplate here
But I don't understand where the $user is coming from.
So looking at the boilerplate we have two files:
The ProfileController here
Excerpt below:
use App\Repositories\Frontend\Access\User\UserRepository;
/**
* Class ProfileController.
*/
class ProfileController extends Controller
{
/**
* #var UserRepository
*/
protected $user;
/**
* ProfileController constructor.
*
* #param UserRepository $user
*/
public function __construct(UserRepository $user)
{
$this->user = $user;
}
This looks a lot like the dependency injection mentioned in the docs, which is this:
class UserController extends Controller {
/**
* The user repository instance.
*/
protected $users;
/**
* Create a new controller instance.
*
* #param UserRepository $users
* #return void
*/
public function __construct(UserRepository $users)
{
$this->users = $users;
}
My problem is I don't understand where the $user is coming from.
In the UserRepository there is no $user defined as a parameter of the class itself. No where in the code is there any Auth::user() so I'm confused as to where the user instance is coming from.
In Laravel dependency injection is handled by the Container. I'm simplifying, but you can think of the container as a source of objects. If there is a singleton, its stored in the container. Otherwise the container knows how to instantiate objects for you. Whenever Laravel calls a method (like in a controller) or instantiates an object for you it will inspect the constructor and look for type hinted dependencies. If it sees a dependency it knows how to retrieve or create it will do so and pass it in for you.
So when Laravel instantiates the controller it looks at the constructor
public function __construct(UserRepository $user)
{
$this->user = $user;
}
The container uses Type Hinting to see that it requires a UserRepository so it will instantiate a new one for you. It also does this recursively. So when it creates a new UserRepository it looks at that constructor and sees that it requires a RoleRepository so it will instantiate that as well.
TLDR: The service container inspects your dependencies and will instantiate them for you.
Welcome to the dubious magic of Laravel. The basic idea with these dependency injections is that, depending on how you define your routes & controllers, Laravel can perform some automagical parsing of urls, identification of ids in those urls, and database fetching of objects.
My problem is I don't understand where the $user is coming from.
You should probably read the docs on the service container. You can also get a better idea of how your route definitions translate into parameter-laden urls with this command:
php artisan route:list
On one of my projects, this results in this output:
+--------+-----------+----------------------------+--------------------+-------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+----------------------------+--------------------+-------------------------------------------------+--------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | GET|HEAD | categories | categories.index | App\Http\Controllers\CategoryController#index | web |
| | POST | categories | categories.store | App\Http\Controllers\CategoryController#store | web |
| | GET|HEAD | categories/create | categories.create | App\Http\Controllers\CategoryController#create | web |
| | GET|HEAD | categories/{category} | categories.show | App\Http\Controllers\CategoryController#show | web |
| | PUT|PATCH | categories/{category} | categories.update | App\Http\Controllers\CategoryController#update | web |
| | DELETE | categories/{category} | categories.destroy | App\Http\Controllers\CategoryController#destroy | web |
| | GET|HEAD | categories/{category}/edit | categories.edit | App\Http\Controllers\CategoryController#edit | web |
| | GET|HEAD | products | products.index | App\Http\Controllers\ProductController#index | web |
| | POST | products | products.store | App\Http\Controllers\ProductController#store | web |
| | GET|HEAD | products/create | products.create | App\Http\Controllers\ProductController#create | web |
| | GET|HEAD | products/{product} | products.show | App\Http\Controllers\ProductController#show | web |
| | PUT|PATCH | products/{product} | products.update | App\Http\Controllers\ProductController#update | web |
| | DELETE | products/{product} | products.destroy | App\Http\Controllers\ProductController#destroy | web |
| | GET|HEAD | products/{product}/edit | products.edit | App\Http\Controllers\ProductController#edit | web |
+--------+-----------+----------------------------+--------------------+-------------------------------------------------+--------------+
And all those routes and their uris and parameters are generated from only a couple of very simple routes definitions. Here's my routes file:
$ cat routes/web.php
<?php
Route::get('/', function () {
return view('master');
});
Route::resource('products', 'ProductController');
Route::resource('categories', 'CategoryController');
If you look at the list of URIs in the routes output above, you'll see parameters named in the URIs like {category} and {product}. These correspond to ids/keys in the URI which Laravel identifies. Laravel is "smart" enough to look at my Controller files, see the type-hinting in the various functions and detect that my function is expecting a depedency to be injected.
For instance, the Category controller's show method looks like this:
public function show(Tree $category)
{
var_dump($category);
}
My controller might seem a little unusual because I'm type hinting that I want an object of type Tree, but Laravel is smart enough to recognize that I do in fact want a Model of type Tree, so it parses out the url and finds the id in it and automatically fetches the record in my db table trees with id matching the {category} fragment of my url and injects that into my function.
Note that I had some trouble when I tried to name the input parameter $tree instead of $category. That other thread may help answer your question a bit too.
The bottom line is that Laravel does a lot of "magic" to hopefully free you up from the tedious of manually defining your own code and queries to retrieve the objects you want.
In my public/index.php of a Laravel 5 application, I have to query some fields in my database, so I used DB::talbe() to do that.
But it returns error:
Fatal error: Class 'DB' not found in C:\xampp\htdocs\oceanboost\public\index.php on line 49
The code I used to call is:
$_active_plugins = DB::table("option")->where("key", "_active_plugins")->first();
I tried to use
$_active_plugins = \DB::table("option")->where("key", "_active_plugins")->first();
but the same error
And here is my full code of public/index.php
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* #package Laravel
* #author Taylor Otwell <taylorotwell#gmail.com>
*/
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels nice to relax.
|
*/
require __DIR__.'/../bootstrap/autoload.php';
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
$app = require_once __DIR__.'/../bootstrap/app.php';
/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/
$_active_plugins = DB::table("option")->where("key", "_active_plugins")->first();
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
My question is how to call built-in classes like DB in a file when this file is not a class
It's not possible to use the DB facade at that point. The whole database component is booted from the Kernel at this part:
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
Before that it's not available.
What you should do, is create a Service Provider for that. I won't go into all the details but you basically add a class that has a register and an optional boot method. You can create one with an artisan command:
php artisan make:provider PluginServiceProvider
In there you can use the DB facade like you want to. Then you just need to register that provider in config/app.php by adding it to the long providers array and your code will run before any routing or such happens.
Note that you should put your code into the boot method since this one is called after all other providers have been registered.
#Nguyen, if I am not mistaking, I think you are trying to query the database in the public/index.php, which is a very big mistake when considering MVC Frameworks.
Please to do this, you should query your database in a controller then pass the results to a view which will be returned to the index.php.
And if you are in a dilemma, see laravel docs