Laravel route removing parent route - php

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

Related

Connecting HTML to existing Laravel

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.

laravel making its own route to blade file

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")

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?}'.

Unable to load controllers from another controller

I'm trying to load a controller inside another controller.
$data['com_top_menu'] = $this->load->controller('account/com_top_menu');
However, this seems to not work when I'm trying to load a controller that is located in the same folder as the controller I'm loading it from.
Tried loading controllers from other folders and seem to not load as well. It seams to load only from the 'common' controllers folder.
Edit:
Actually it seems that the controller is loading. If I place an echo in the middle of the loaded controller it will show the output before the template rendered. So, it looks like the controller is loaded and just doesn't output anything through the rendered view, unless it is a controller inside the common folder.
Files are all in place, controller loads, it just doesn't output anything through the view.
Few things for to load controllers-
1st - you can only load controller from same folders (admin/ catalog).
2nd - you can load controller from any subfolder, just need to pass correct loading path.
3rd - If Opencart hasn't that file than it will not display any error, result will be null/ false.
4th - If you are defining any function name then it will call that function else will call index function so in your case index.
5th - Please use this
return $this->load->view('your.tpl', $data);
Instead of
$this->response->setOutput($this->load->view('your.tpl', $data));
6th - Please enable your debug mode from php/ admin so that you will know any error if your code is throwing. Clear your error.log and then try to load controller.
7th - If these all points are code is not working then do 1 thing - add a blank controller with index function and just add one line so that you can return it's result from view then just
echo 'here';
In your view. If OC is not returning this result it's mean you have error in Opencart files else there is error in your code.
You can say these are same in a way (i am not saying completely and don't want to hurt anyone feelings ;)) but this code
$this->load->controller('account/com_top_menu');
is equal to (based on your autoloader)
$obj = new ComTopMenu; //assuming your class name
$data['com_top_menu'] = $obj->index();
so for your solution please check
- you have file com_top_menu.php in your catalog > controller > account >
- your file class name must be ControllerAccountComTopMenu (or any uppercase or lowercase combination but without _)
- your class must have index function because in your case it calling index.

Only allow URL's specified in routes to be seen in Codeigniter

If I have a controller called articles, which has a method called view_articles, a user can type in http://example.com/articles/view_articles/some-post and have it return a page.
I have specified a route to be http://example.com/article/post-name. How can I make it so that only the URL specified in the route is visible? Is there a way for articles/view_articles/some-post to show a 404 instead of showing the same page as the route URL?
I am trying to prevent duplication for SEO purposes.
You can always make default routing to a 404 page by correctly defining routes in your routes.php file:
$routes['article/(:any)'] = 'articles/view_articles/$1';
$routes['(:any)'] = 'main/e404';
As stated by CodeIgniter user guide:
Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.
So you can basically define all you want to be seen at the beginning of the file and block everything else on the last line.
As for your main(can be any other) controller's 404 method -
function e404() {
show_404();
}
Use $this->uri->segment(n) as part of the URI class inside of view_articles to redirect traffic to your route if the URI contains view_articles as it's second segment.
I'v done that in other tricky way, first of all you should add some code to __construct function in sys/core/Controller.php
you can check whether the requested url is in routes or not by this code
if(!isset($this->router->routes[uri_string()])){
show_404(); // Or whatever you want ...
}

Categories