Connecting HTML to existing Laravel - php

I have installed Laravel script which has his own front page (homepage) which i dont like and wanted to replace with my own html. I moved css, images to public folder, renames index.html to index.blade.php (moved to resources/views) and added routing - Route::get('/index2', function () {return view('index2');}); but it does not load my homepage. I changed all links to css inside my home.blade.php like this - /assets/css/styles.css" rel="stylesheet"> .
There is an existing routing -Route::get('/','HomeController#index'); which loads old homepage. Do i remove it or leave it be?
And why it's named different - homecontroller#index?
Thank you for your help!

First of all, remove the default Route from web.php and Add below code
Route::get('/', function () {
return view('index'); //Change blade as per your file name
})->name('home');
homecontroller#index This is the default controller and index method which you get. Basically in index method returns the default welcome page blade. You can use that as well if you want.
Divide your HTML code into Separate small components. So you can extend the same code in different files.
Use a named route instead of a direct URL in like this.

Related

Laravel route removing parent route

I have a laravel project which I am working on, the public folder of the laravel project is in a sub folder
i.e. htdocs/laravel_folder/public_folder and files
so in the web browser the address is www.website.com/laravel_folder/laravel_page_etc
I have been able to load the index file but when i want to load another file it removes the parent folder(i.e. laravel_folder)
so the uri become
www.website.com/page_to_be_loaded instead of www.website.com/laravel_folder/page_to_be_loaded
This is my controller
public function load_form(Request $request) {
//return view('blade_file_to_load');// I have tried this
return redirect()
->route('route_to_load')
->guest('blade_file_to_load');//i tried this one also
}
this is the web.php
Route::get('/', 'MyController#index'); //this one show the index page when i load "laravel_folder"
Route::get('/form_load', 'MyController#load_form');
Route::post('/form_load', 'MyController#insert_record');
Basically when i try to load "laravel_folder/form_load" it would show in the browser address bar "form_load" and page not found
Update: But when i try the following route
'www.website.com/laravel_folder/page_to_be_loaded' it tries to load the index page, and this produces an error because the page is expecting a data which is not presented to it
www.website.com/laravel_folder/page_to_be_loaded / with the / added to the end of it it would remove the laravel_folder as I stated earlier.
Update 2 I changed the .env variable of APP_URL localhost to http:// www.website.com/folder_name but it is still doing the same thing

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

Controller returning application/json instead of view

I have a very weird problem. I could access the page and everything was fine until i added a few new routes in my web.php routing file. Problem is with 5th route(named post.create). The ** are just to highlight the line/route i am talking about:
Route::group(['prefix'=>'admin', 'middleware'=>'auth'], function()
{
Route::get('home', 'HomeController#index')->name('admin.home');
Route::get('post/all','PostsController#index')->name("post.all");
Route::get('post/{id?}','PostsController#show')->name('post.fetch');
**Route::get('post/create','PostsController#create')->name('post.create');**
Route::post('post/store', 'PostsController#store')->name('post.store');
Route::put('post/{id?}','PostsController#update')->name('post.update');
Route::delete('post/delete/{id}','PostsController#destroy')->name('post.delete');
Route::get('category/create','CategoriesController#create')->name('category.create');
Route::post('category/store','CategoriesController#store')->name('category.store');
Route::get('category/all','CategoriesController#index')->name('category.all');
Route::get('category/{id?}','CategoriesController#show')->name('category.fetch');
Route::delete('category/delete/{id}','CategoriesController#destroy')->name('category.delete');
Route::put('category/{id}','CategoriesController#update')->name('category.update');
});
When i am accessing this route i get a blank page with a pair of curly braces only, nothing else. There is a message on the browser console that says - Resource interpreted as Document but transferred with MIME type application/json.
But if i change the route to
Route::get('posts/create','PostsController#create')->name('post.create');
,which is just add an additional s, i get the full view of the page.
I cannot seem to figure out why the earlier route is sending back application/json(seems an empty object). I made no change to the controller function. Here is the code for the PostsController#create function:
public function create()
{
$categories = Category::all();
return view('admin.posts.create', compact('categories'));
}
I have tried to return a different view or a simple string from this function for this route. Nothing seems to work.
What am i doing wrong, can anyone please help?
Laravel will serve the first route matched in the order you define them. Since you have post.fetch first it is serving that route with 'create' as the id parameter.
In your routes file place post.create before post.fetch so you have:
Route::get('post/create','PostsController#create')->name('post.create');
Route::get('post/{id?}','PostsController#show')->name('post.fetch');
Route::post('post/store', 'PostsController#store')->name('post.store');
Route::put('post/{id?}','PostsController#update')->name('post.update');
You should name Blade file as:
resources/views/admin/posts/create.blade.php
Blade view files use the .blade.php file extension and are typically stored in the resources/views directory
https://laravel.com/docs/5.4/blade#introduction
Update
In comments I've recommended you to move the route before the 'post/{id?}'.

Change laravel welcome file name

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

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');

Categories