Laravel Route Controller issue - laravel

I am trying to add a new route to my application and can't seem to get it to work. I keep getting a 404 error. It looks like the physical path is looking at the wrong directory. Currently looking at D:\Web\FormMapper\blog\public\forms but should be looking at D:\Web\FormMapper\blog\resources\view\layout\pages\forms.blade.php
My request URL:
http://localhost/FormMapper/ /works fine
http://localhost/FormMapper/forms /doesn't work
http://localhost/FormMapper/forms.php /No input file specified.
my FormsController:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FormsController extends Controller
{
public function index()
{
return view('layouts.pages.forms');
}
}
My web.php:
Route::get('/', function () {
return view('layouts/pages/login');
});
Route::get('/forms', 'FormsController#index');
My folder structure looks like this:
My config/view.php
return [
'paths' => [
resource_path('views'),
],
'compiled' => env(
'VIEW_COMPILED_PATH',
realpath(storage_path('framework/views'))
),
];

you must use dot for this. In your controller change to this:
return view('layouts.pages.forms');

If your route only needs to return a view, you may use the Route::view method. Like the redirect method, this method provides a simple shortcut so that you do not have to define a full route or controller. The view method accepts a URI as its first argument and a view name as its second argument. In addition, you may provide an array of data to pass to the view as an optional third argument:
Route::view('/', 'layouts.pages.login');
Route::view('/forms', 'layouts.pages.forms', ['foo' => 'bar']);
Check docs

After tracking digging deeper I determined that the issue was that IIS requires URL rewrite rules in place for Laravel to work properly. The index.php and '/' route would work b/c it was the default page but any other pages wouldn't. To test this I used the
php artisan serve
approach to it. and everything worked properly. Unfortunately I am unable to do this in production so I needed to get it to work with IIS.

Related

Using route actions with new laravel route syntax

I have question about syntax which I can not find an answer for.
I have code in routes file:
Route::get(
'/something/{seoString}/{someMore}',
['as' => 'my_name', 'uses' => '\my\namespace\MyController#index', 'my_route_action' => 20]
);
And I would like to rewrite it using the new syntax for calling controller
Route::get(
'/something/{seoString}/{someMore}',
[MyController#::class, 'index'] // would like to use this new syntax
);
And it works fine, but how can I add the custom route action 'my_route_action'?
I know it's possible to wrap the routes with a group and add it this way:
Route::group(['my_route_action' => 20], static function () {
Route::get(
'/something/{seoString}/{someMore}',
[MyController#::class, 'index'] // would like to use this new syntax
);
);
But that's not what I'm looking for. I don't want to be adding one group for each route just to add the route action.
So I wanted to ask if it does exist something like ->addCustomAction() or how is this supposed to be done?
Unfortunately the route action is not a thing, and probably shouldn't be. Unsure what you're actually trying to achieve with that too.
If you're passing in GET data like a bit of data, you can do it through: {variable} so the URL would become the following:
Route::get('my-route-url/{model}/get', [MyController::class, 'methodName')->name('something')->middleware(['something'])
And in your controller, you dependency inject request if you're wanting to use that too, as well as the model:
public function methodName(Request $request, Model $model)
{
dd($request->all(), $model);
}
The "as" is the the name method. Middleware is still middleware.
If you're trying to do a Key/Pair bit of data, you need to use POST request and pass it in the data, which you can access via the $request->input('keyName') method in the controller.

Laravel PUT routes return 404

I'm trying to update data in the database
but it returns error 404 not found in postman
Route::put('/products/{id}','productController#update');
Just add 'Accept: application/json' header to request.
Please provide more code so we can find exactly where your problem is.
I assume you wrote the route in the api.php file and not in web.php file.
If you do so, you must enter the route as api/products/1.
You have to be aware of the route group prefix as well if you are using it. Every routes inside a group prefix will be wrapped and requires the prefix string in the beginning every time you want to access it.
For instance:
in web.php file:
Route::group(['prefix' => 'api'], function () {
Route::put('products/{id}', 'productController#update');
});
# this will require you to access the url by tyiping "api/products/1".
and in the api php file (this is the more likely for new users have to be aware):
Route::group(['prefix' => 'api'], function () {
Route::put('products/{id}', 'productController#update');
});
# this will require you to access the url by tyiping "api/api/products/1" since the api.php file will automatically wrap your routes within an api prefix.
and one more thing you need to know, if you are using a getRoutesKeyName method on your model, you should follow wether the wildcard to use id's or maybe slug based on what you type inside the method.
For instance:
public function getRoutesKeyName(){
return 'slug';
}
# this will require you to type "products/name-of-product" instead of "products/1"

Call to a member function middleware() on null

I use the following middleware in routing Laravel:
Route::group(['prefix' => 'api/'], function() {
Route::resource('admin', 'adminController')->middleware('auth');
Route::resource('profile', 'profileController')->middleware('role');
});
I get this error when i call 'admin' or 'profile' path in URL
Use this
Route::prefix("/dashboard")->middleware(['first','second'])->group(function(){
});
It is because Route::resource() does not return anything. Its void. It doesn't return an object.
Laravel 5.4 - Illuminate\Routing\Router#resource
In Laravel 5.5 (in development), Route::resource() will be returning an object for fluently adding options to.
Simply reverse the order:
Route::middleware('scope:clock-in')->resource('clock', 'ClockController');
As lagbox stated:
Route::resource() does not return anything.
However middleware does.
Most likely your resource controller is not resolving to an actual controller. Some things to check
Check you actually have an adminController, and that the class name is in the correct case
Check that your controller is in the default namespace and, if not, change the controller namespace, or add a namespace attribute to your route
Check that your controller is not causing an exception on start that is being ignored resulting in you having a null controller.
In Laravel 8.x you can solve this problem with:
Route::group(['middleware' => 'auth','middleware' => 'role'], function () {
..........
});

Laravel passing all routes for a particular domain to a controller

Working on a Laravel 4.2 project. What I am trying to accomplish is pass every URI pattern to a controller that I can then go to the database and see if I need to redirect this URL (I know I can do this simple in PHP and do not need to go through Laravel, but just trying to use this as a learning experience.)
So what I have at the moment is this:
Route::group(array('domain' => 'sub.domain.com'), function()
{
Route::get('?', 'RedirectController#index');
});
I am routing any subdomain which I deem as a "redirect subdomain" ... The ? is where I am having the problem. From what I have read you should be able to use "*" for anything but that does not seem to be working. Anyone have a clue how to pass any URL to a controller?
And on top of that I would ideally like to pass the FULL URL so i can easily just check the DB and redirect so:
$url = URL::full();
Try this:
Route::group(array('domain' => 'sub.domain.com'), function()
{
Route::get('{path}', 'RedirectController#index')
->where('path', '.*');
});
And your controller will reseive the path as first argument
public function index($path){
// ...
}
In case you're wondering, the where is needed because without it {path} will only match the path until the first /. This way all characters, even /, are allowed as route parameter

Why aren't my controllers actions accessible with unit tests in Laravel

I have set up my route as such:
Route::controller('clients', 'Controllers\ClientsController');
Through this method I can easily access all the controller functions via post and get. However I cannot test them as easily.
public function testCantDeleteOtherAccountsClient()
{
Route::enableFilters();
$user = Models\User::find(1);
$this->be($user);
$response = $this->action('GET', 'ClientsController#getDelete');
$this->assertRedirectedToAction('ClientsController#getIndex');
}
This test results in the message
InvalidArgumentException: Route [ClientsController#getDelete] not defined.
The method accessible via url though. What am I missing?
Just tried this out myself (a route specified via the controller) your issue is that using action requires a named route. Controller routes do not currently support this as far as I'm aware of.
If you create a test route:
Route::get('test', array(
'as' => 'testName',
'uses' => 'ClientsController#getDelete'
));
And try
$this->action('GET', 'testName');
The test should pass, you can view all the routes with names via php artisan routes.
You may want to use $this->client->request() instead. You can check if a redirect occurred with:
$this->assertRedirectedTo("some\url");
Note that $this->call() is just an alias to $this->client->request().
I found changing it to use call instead of action worked for me:
$response = $this->call('GET', 'clients/delete/1');

Categories

Resources