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
Related
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")
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');
I'm fairly new to Laravel. I'm having a problem with routing.
Route::group(['prefix'=>'api/v1'],function(){
Route::resource('results','RequestController');
Route::get('results/getByName/{name}','RequestController#getByName');
Route::get('results/getLastTen','RequestController#getLastTen');
});
The problem is that the last route under prefix api/v1 does not work. When I call it it shows nothing, not even any error.
The code at the requestController is:
public function getLastTen(){
$results=DB::table('latest_random_trends')->limit(10)->get();
return $results;
}
Everything is alright with the code on the controller since it works when I call it from the routes.php file outside of the prefix 'api/v1' like this:
Route::get('results/getLastTen','RequestController#getLastTen');
but when it is inside the prefix it does not work unless I add a variable to it like this:
Route::get('results/getLastTen/{var}','RequestController#getLastTen');
Since you have a Route::resource above it, I think what's happening is that the show method on Resource Controller is getting the route instead of the one you wrote.
Try one the following:
Exclude the show method if you're not going to use it
Route::resource('results','RequestController', ['except' => 'show']);
Move your custom route above the resource route
Route::group(['prefix'=>'api/v1'],function(){
Route::get('results/getLastTen','RequestController#getLastTen');
Route::resource('results','RequestController');
Route::get('results/getByName/{name}', 'RequestController#getByName');
});
For more information, check out the show action on Laravel Docs
I changed the files path after i installed laravel framework like this:
from:
resources/views/welcome.bandle.php
to
resources/views/admin/index.php
and the routes file to:
Route::get('/admin', function () {
return view('admin/index');
});
the url is working
but all the larvael render not working
like this:
what i need to do?
tnx a lot.
You need to add the .blade.php extension to the files you want to parse using "Blade Engine", that will remove all the tags you have within curly braces.
Next, you need to write your route like this:
Route::get('/admin', function(){
return view('admin.index');
})->name('admin.index')->middleware('auth');
It is a good convention naming the routes for easy access across the application, that way you can simply reference it in the blade views like this:
Admin page
That way you will have the dynamic route no matter from where in the file structure you call it.
Or you can also use your Controller to display such view. By this you'll write your routes more cleaner. Let's say we have an AdminController that handles all admin processes and functions. Put your dashboard.blade.php inside views/admin directory.
The route:
Route::get('/admin', 'AdminController#index');
The controller:
class AdminController extends Controller
{
public function index()
{
return view('admin.dashboard'); // in views->admin->dashboard.blade.php
//add some data here
}
}
Just keep 'blade' in view file name if you don't plan to use controller, e.g.:
resources/views/admin/index.blade.php
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');