laravel making its own route to blade file - php

working a project with Laravel 5.6.
the problem is, when im going to an url like:
/login
or any other route and specify where it should go, it making its own route and going to another place. no metter if i even clear the code of that blade file.
i have not several routes or blade file that are the same. i have cleared my browser cache, laravel cach, config cache, and the command:
php artisan route:cache
did not worked to clear route cache.
my code example: web.php code
Route::get("/login", "LoginController#login");
Example: LoginController.php code
public function login()
{
return view('/login'); // not going to this path
}
to conclude, it does not read my code :(
need your ideas!

public function login()
{
return view('login');
}
view accepts the view name not the path of the route, If you want go to any route use redirect("/route_name") . But in your case if you redirect to login route again it will throw exception because the login route again calls this function. so you need to pass the view name. for example:
if your login view is in
resources
- views
-login.blade.php
then use above code. or if the login page is in any other folder inside view
it will be like return view("foldername.login")

Related

Laravel 8 Auth middleware protected route failing

I am building my first Laravel app with the Metronic 8 Laravel theme. It uses Breeze for authentication. I changed a couple of things around - created a welcome page for non-logged-in users, and moved the main template that was the index to an auth protected "/dashboard". The problem is that it still tries to load the dashboard Blade template, regardless of authentication, resulting in an error.
Route
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth'])->name('dashboard');
Here's Authenticate, where it should redirect non-authenticated users to the login page.
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
When I'm not logged in and navigate to the dashboard URL, it attempts to load the dashboard Blade template, which calls a menu function that checks the user permissions for menu items. Unfortunately, since there is no user, the application blows up from passing a null value to a method expecting a user array/object.
Any ideas on where to look for the problem? It seems to me that the auth middleware should redirect to the login page before trying to load the Blade template when not logged in.
I would put the middleware at the beginning of the route like this, though I'm sure it's not causing the problem-
Route::middleware(['auth'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Aside from that, please provide some information on the error itself like what the error is about/what is says..etc...
First of all, make sure you have a login named route defined in your routes/web.php file. It should look something like:
Route::get('/login', '<controller>#<method>')->name('login');
The important bit is ->name('login') so that the Authenticate middleware can correctly identify the route to redirect to. Change <controller>#<method> appropriately to route to the login method of your app.
Wakil's answer is irrelevant and actually opposite of the documentation. Your syntax is correct.
I figured out the issue. Keen Themes put a call to a method to build an array of menu items in the web routes file. That was making the call to the offending code. After I wrapped that in an auth check the error was fixed, and everything works as expected.

Laravel 7: Generate named view-routes from blade files in directory

Is there a nice way, to solve this issue: I have a folder ressources/views/project/content with several blade teplates, let's say:
home.blade.php
how-to.blade.php
info.blade.php
best-way-to-score.blade.php
...
Right now, I define one view route per file:
Route::view('/home', 'project.content.home')->name('home');
Route::view('/how-to', 'project.content.how-to')->name('how-to');
...
How can I create these routes on thy fly? I could solve it with a loop through all files in this directory, but maybe there is a more elegant way/function in laravel I don't know yet?
If I understand correctly, what you you need is a generic get route like this:
Route::get('/{page}', 'PageController#show');
and then you need a PageController with a function to return the requested page:
public function show($page)
{
return view('project.content.'.$page);
}
Just have in mind that this kind of route will "catch" every get request so put it at the end of the web.php file

I am unable to access Laravel view

I have just created one of about view in view/about.blade.php, and I am accessing this from localhost/myproject/public/about, but it's not working.
However, localhost/myprojects/public/ is working fine; about view has been created on same parameters as welcome by default in Laravel.
Firstly the information is not sufficient to say anything.Please provide your route.Also its important how you are running your project ,is it via Xampp(or Lampp whatever is there) or "php artisan serve"
but looking from your working directory "localhost/myprojects/public" I guess its not by the command . Try localhost/myprojects/public/about.blade.php or run it by php artisan serve and try route localhost:8000/about
Have you added particular routing to web.php file?
Route::get('about', function () {
return view('about');
});
https://laravel.com/docs/5.7/routing
Which error are you getting?
404 - Not found
Route::get('/about', function () {
return view('about');
});
Check routes
php artisan route:list
Laravel is a MVC Framework, Which means You Have a Controller which procede some logic when some request come in and interact with the model if need, after that the controller return some view.
And because you whan to acccess your view file, you must past through controller, and that controller will render the view. Because the views folder is not in the \public dicretory as subdirectory you can't access to It with url like localhost/myproject/public/about even if you get access to it, you will not get HTML, you'll get some plain text with Blade tags. It's a must to return view in you controller by rendering it, somewhere in the background Laravel procede all Blade Tag and return HTML that correspond to that tags.
What I can suggest you Is to create some route in your route file like this
Route::get('/about', function(Request $request){
// Automatically Laravel will look this file in the view directory
return view('about');
});
Or you can go with the controller like procedure by creating some controller, go in your terminal and execute
php artisan make:controller AboutController
this will generate a file name AboutController.php in app\Http\Controllers diretory within witch you will found
namespace App\Http\Controllers;
class HomeController extends Controller
{
}
after that add
public function index()
{
return View::make('about');
}
Don't forget to include use the Illuminale\Supports\Facades\View on top of your file
And one more important thing which left is to configure the Route, for that go in routes directory in the web.php file add
Route::get('/about', 'AboutController#index')->name('about');

How to add a new page to the admin view of laravel framework

I have a website in laravel framework and I am trying to add a simple new static page to the admin panel. I have done the following three steps:
Add a template to the views:
app/views/admin/MessageToAll.blade.php
Add the make view code in the controller.
public function MessageToAll(){
return View::make('admin.MessageToAll');
}
Added a route in app/routes.php
Route::get('/admin/MessageToAll',array('as'=>'MessageToAll','uses'=>'AdminController#MessageToAll'));
But when I go to to domain.com/admin/MessageToAll
it gives me a 404 page not found error. Does anyone know what have I missed as I think I have completed all steps for adding this view.
Just put your new route before the /admin/ route (to test it, you want to temporarily make it the very first route in routes.php). The problem is /admin/ or some other similar route executed before your new route.
Also, if you need to just execute static view, you can use something like this (works without using a controller):
Route::get('/admin/MessageToAll', function (){
return View::make('admin.MessageToAll');
});
in routes add:
Route::get('/admin/MessageToAll','yourController#yourMethod');

Laravel 5.1 routing returning wrong content from controller

I have this really weird problem with laravels routing.
I started to make some routes and controllers and just returning strings from each controller confirm that it worked.
And everything did work.
Now when i started making the master view and putting it together with some templates for the routes I noticed that the string that laravel returns isn't the string i wrote.
All routes return "This is routename page"
The only routes that actually work as expected is the routes with wild cards, and the route going to the start page.
Those routes return the correct strings.
Example routing
Route::get('/users', 'UserController#index');
class UserController extends BaseController {
public function index() {
return 'List of users!';
}
});
This routing displays "This is user page" (NO ERROR)
I have tried returning the string directly from the route, clearing all the cache files i could find including route cache, restarting browser and MAMP
Just to be clear, the routing returned the correct strings when I made the route.
I have installed Elixir to compile my scss files, but i doubt that should have anything to do with my problem.. :(
Figured it out just after I posted the question!
I had a route with a wildcard directly after the root
Route::get('/{'user'});
This route were overriding all other routes that only had one parameter after the root. So if I go to the url "/users" the route will assume it is a wildcard and send it to another controller that returns the string "This is {wildcard} page!", Brainfreez! :P

Categories