Laravel route allow any parameter not working - php

Any help why this is not working,I am using Laravel 5.4 version ,this is are my routes
app\Providers\RouteServiceProvider.php
public function map()
{
$this->mapWebRoutes();
$this->mapExampleRoutes();
}
protected function mapExampleRoutes()
{
Route::prefix('example')
->middleware('example')
->namespace($this->namespace.'\\Examle')
->group(base_path('routes/example.php'));
}
routes\example.php
Route::get('/{any}', function () {
return view('example.app');
})->where('any', '.*');
$ php artisan route:list
+--------+-----------+-----------------+------+----------+-------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+-----------------+------+----------+-------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | example/{any} | | Closure | example |
+--------+-----------+-----------------+------+----------+-------------+
The problem is when I try to access /example it returns not found (NotFoundHttpException) ,
other routes are working , for example, /example/login .
any idea why this one is not working ?

Route::get('{any?}', function () {
return view('example.app');
})->where('any', '.*');
I removed the leading slash (/) and added a question mark (?) to indicate the slug is optional.

Related

laravel - how to add prefix in all url without affecting route existing

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.

Framework PHP Route - Error 404 Not found

In Laravel, I need to edit user created by Auth/RegisterController, but using the controller that I created, example ProfileController. But when I try access by a a:href button, show 404 Error.
Code:
web.php
Route::group(['middleware' => ['auth']], function(){
Route::resource('profile', 'ProfileController')->except(['edit']);
Route::get('profile/{profile}/edit', ['as' => 'profile.edit', 'uses' => 'ProfileController#edit']);
});
Auth::routes();
app.blade.php
<a href="{{route('profile.edit',['profile'=>auth()->user()->id])}}" class="btn btn-primary">
{{auth()->user()->email}}
</a>
ProfileController
public function edit($profile)
{
$user = \App\User::findOrFail($profile);
return view('profile.edit', compact('user'));
}
List Routes
| | POST | profile | profile.store | App\Http\Controllers\ProfileController#store | web,auth,guest |
| | GET|HEAD | profile | profile.index | App\Http\Controllers\ProfileController#index | web,auth,guest |
| | GET|HEAD | profile/create | profile.create | App\Http\Controllers\ProfileController#create | web,auth,guest |
| | DELETE | profile/{profile} | profile.destroy | App\Http\Controllers\ProfileController#destroy | web,auth,guest |
| | PUT|PATCH | profile/{profile} | profile.update | App\Http\Controllers\ProfileController#update | web,auth,guest |
| | GET|HEAD | profile/{profile} | profile.show | App\Http\Controllers\ProfileController#show | web,auth,guest |
| | GET|HEAD | profile/{profile}/edit | profile.edit | App\Http\Controllers\ProfileController#edit | web,auth,guest |
Someone can help me?
Tks,
Aguiar, Adson M.
Welcome to SO!
Like #lagbox mentioned, you can just keep the
Route::resource('profile', 'ProfileController');
without the ->except() to have the exact same edit route automatically generated for you.
Furthermore, you can simplify your controller action to:
public function edit(User $profile)
{
return view('profile.edit', compact('profile'));
}
This uses type hinting to automatically have the dependency injection resolve a User instance for you, identified by the key passed in with the {profile} parameter.
You would then have to go with $profile in your profile/edit.blade.php, unless you want to do something like return view('profile.edit', ['user' => $profile]);.
It might be a good idea to name the parameters like your Models. Resource routes usually base completely on the Model's names, so profile is not a good choice for User routes. You might either use Route::resource('user', 'ProfileController') (or even UserController for that matter), or if you want to stick with profile, but want $user as a parameter, you would have to create all routes by yourself like Route::get('profile/{user}/edit', 'ProfileController').
Also, writing route('profile.edit',['profile'=>auth()->user()]) works too, and has the advantage that it would not break when the user is not authenticated.
All that said, your code should still work. Try to use a find() instead of a findOrFail() and dd($user); in the following line to make sure your route is working. If you see the dump, it is probably null, meaning the id passed is not valid, which would be really odd.
If you can post the full generated link of the anchor tag and the outcome of the dd, I'll be happy to help.

Laravel 5.1 adding middleware to resource route

so I have been trying to use middleware with my route resource and having trouble making it work.
Here is my routes setup:
Route::group(['prefix' => 'api','middleware' => 'locationRouteValidator'], function()
{
Route::resource('location', 'LocationController');
});
and route seems to be setup properly:
php artisan route:list
+--------+----------+------------------------------+----------------------+-------------------------------------------------+------------------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+------------------------------+----------------------+-------------------------------------------------+------------------------+
| | GET|HEAD | / | | Closure | |
| | GET|HEAD | api/location | api.location.index | App\Http\Controllers\LocationController#index | locationRouteValidator |
| | POST | api/location | api.location.store | App\Http\Controllers\LocationController#store | locationRouteValidator |
| | GET|HEAD | api/location/create | api.location.create | App\Http\Controllers\LocationController#create | locationRouteValidator |
| | DELETE | api/location/{location} | api.location.destroy | App\Http\Controllers\LocationController#destroy | locationRouteValidator |
| | PATCH | api/location/{location} | | App\Http\Controllers\LocationController#update | locationRouteValidator |
| | GET|HEAD | api/location/{location} | api.location.show | App\Http\Controllers\LocationController#show | locationRouteValidator |
| | PUT | api/location/{location} | api.location.update | App\Http\Controllers\LocationController#update | locationRouteValidator |
| | GET|HEAD | api/location/{location}/edit | api.location.edit | App\Http\Controllers\LocationController#edit | locationRouteValidator |
+--------+----------+------------------------------+----------------------+-------------------------------------------------+------------------------+
so now I create the middleware :
php artisan make:middleware locationRouteValidator
and leave the default code, which is :
public function handle($request, Closure $next)
{
return $next($request);
}
and just for testing, in my controllers show method, I echo out the passed id like so:
public function show($id)
{
//
echo "show ".$id;
}
so now I expect that when I visit /public/api/location/abcd it should display:
show abcd or when I visit /public/api/location/1234 it should display show 1234 after which I intended to modify the middleware to allow only numeric values to be passed into {location}.
But If I just run with the default middleware code, the page returns white without displaying anything. I remove the middleware from the route, and it displays the text as expected.
I know I could attach the middleware to the controller instead, but I thought of attaching it in the route instead so that I could write and apply some common middleware by using the route's group feature, which should be possible, right?
Where do you guys think I am going wrong? Thanks in advance for looking!
Check your \app\http\kernel.php file to see if you have registered the middleware as a route middleware.

Laravel controller action returns error 404

I'm getting a 404 error when trying to access a route linked to a controller action.
I have the route defined like this in my routes.php file.
Route::controller('error', 'ErrorsController');
The ErrorsController class looks as follows.
class ErrorsController extends BaseController {
public function __construct()
{
// vacio
}
public function getIndex()
{
return View::make('error.accessdenied');
}
public function getAccessDenied()
{
return View::make('error.accessdenied');
}
}
I have a view with a link to chek if it is working properly. The link is created as follows
{{ HTML::linkAction('ErrorsController#getAccessDenied', 'Error') }}
When I click on the link the page moves to the URL 'mytestdomain.com/error/access-denied' returning an 404 error, but when I access the URL 'mytestdomain.com/error' it works perfectly.
Any idea on what I'm doing wrong?
EDIT:
Running the command php artisan routes these are the routes pointing to ErrorsController:
+--------+------------------------------------------------------------------------------------------------+------+--------------------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+------------------------------------------------------------------------------------------------+------+--------------------------------------+----------------+---------------+
| | GET|HEAD error/index/{one?}/{two?}/{three?}/{four?}/{five?} | | ErrorsController#getIndex | | |
| | GET|HEAD error | | ErrorsController#getIndex | | |
| | GET|HEAD error/access-denied/{one?}/{two?}/{three?}/{four?}/{five?} | | ErrorsController#getAccessDenied | | |
| | GET|HEAD|POST|PUT|PATCH|DELETE error/{_missing} | | ErrorsController#missingMethod | | |
+--------+------------------------------------------------------------------------------------------------+------+--------------------------------------+----------------+---------------+
Only the sencond and the fourth ones are working.
It looks as though specifying the route in the way you have won't work. This type of routing only works for RESTful requests. See >http://laravel.com/docs/4.2/controllers#restful-resource-controllers>.
You might have to explicitly specify the route using Route::get/post.
Somehow I found the problem.
For some reason, my apache server doesn't rewrite mytestdomain.com/error/ * route. Probably is something related with the word error and the apache module mod_rewrite.
Anyway, defining the route as follows solves the problem.
Route::controller('fail', 'ErrorsController');

Laravel Using controller Route

i have resource in route and that work correctly and i want to change that to Route::controller.
but after define that i get error in php artisan route :
+--------+------------------------------------+-----------+---------------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+------------------------------------+-----------+---------------------------------+----------------+---------------+
| | GET index | index | Closure | | |
| | GET admin/index | dashboard | Closure | | |
| | GET logout | logout | Closure | | |
| | POST auth | auth | Closure | csrf | |
| | GET login | login | Closure | | |
| | GET admin/admin/profile/{_missing} | | ProfileController#missingMethod | | |
+--------+------------------------------------+-----------+---------------------------------+----------------+---------------+
my Current route is:
Route::resource('profile' , 'ProfileController', array('as'=>'profile') );
and i want to change that to :
Route::controller('admin/profile', 'ProfileController', array('index'=>'profile.index') );
how to resolve this problem?
This is not an error, Resource and Controller routes are completely different things.
Resource routes have a predefined list of routes (index, create, store, delete, update). If you don't have the method set in your controller it will still work, unless someone hit that route.
Controller routes relies on your controller methods:
public function getIndex() {}
public function getCreate() {}
public function postStore() {}
Methods names are predefined as
<http method><your action name>()
If those methods are not present in your controller, Laravel will not show them in your routes list.
So, just create a
public function getIndex() {}
In your controller and run
php artisan route
Again.
Use :
Route::resource('profile, 'ProfileController', array('as' => 'profile', 'names' => array('index' => 'profile.index')));
Instead of either the routes above.

Categories