Sorry in advance, I know it has been asked before but I did not figure out the solution.
I am new to Laravel, still learning and stuck with this issue:
My objective is to add pages in admin and show these pages in frontend.
For the Front part of the website I have this route:
Route::get('/{page}', 'PagesController#show');
so the when you access /about, /contact, /another-page I use the same view
For the Admin part of the website I have this route:
Route::get('/admin', 'AdminController#show');
My problem is that the first route overwrites the second route and I don't know how to avoid this.
I have tried with namespaces and grouping routes, but I get the same outcome.
Thank you
To make it simple this is happening because you have the route with the parameter before the admin route so is going to send the "admin as a parameter of page"
The Simple fix is just put admin route before your "/{page} so it will find admin route first,Something Like this:
Route::get('/admin', 'AdminController#show');
Route::get('/{page}', 'PagesController#show');
But I do not recommend building your routes this way and have specifics pages setup if possible, This way of building routes will mess around with the 404 route not found aswell.
Related
I am trying to implement a basic image fetch system for my website. Already created a route that returns me the image.
what concerns me is that i want that route to be only accessible by certain controllers.
Tried to search it and found out passport might be viable option but it's pretty complex for this app. Are there any possible options ?
EDIT:
Sorry for providing insufficient information. I want the route to be accessible only by CONTROLLERS, not by anyone who enters the route url to address bar. Like using it as an api maybe.
There several ways to achieve that, you can use middleware, you can consider using packages like entrust which also require you to have some knowledge about using middleware. or use laravel Auth
create a table add all the routes in that table and then check the allowed route in AppService provider.
$routename = Request::route()->getName();
$allowed_route = AllowedRoutes::where("route","=",$routename)->count();
if($allowed_route == 0)
exit();
I have been working on laravel and have been doing some routing. I was just wondering on what is the difference between writing the route as:
route::get('roles/{id}/edit',rolesController#edit);
versus
route::get('roles/edit/{id}',rolesController#edit);
One difference is clearly visible and that is the placement of the id variable. Can't figure out any other reason. Please provide an explanation on this.
Other than the actual look of the URL, there's no real difference as far as the framework is concerned.
I suppose it's the matter of preference when using any of this. Maybe, for example, if you are giving options of editing the user profile and posts, this might come handy as both are different routes, technically
No difference. It depends on you how you would like to build your routes. But try to user best practices which laravel creator recommend (https://laravel.com/docs/5.7/controllers#resource-controllers).
And also i want take your attention on how you called your controller. You should use CamelCase for naming your files (https://github.com/alexeymezenin/laravel-best-practices/blob/master/README.md).
There's no difference, but you might want to look in reosource routes and controller. Basically, laravel framework automatically creates routes and methods for controllers that you might need in your project. For example:
If you create a contoller like this:
php artisan make:controller RolesController --resource
and create a resource route like this:
Route::resource('/roles', 'RolesController ');
framework automatically crates this routes for you:
Verb Path Action Route Name
GET /roles index roles.index
GET /roles/create create roles.create
POST /roles store roles.store
GET /roles/{roles} show roles.show
GET /roles/{roles}/edit edit roles.edit
PUT|PATCH /roles/{roles} update roles.update
DELETE /roles/{roles} destroy roles.destroy
So you don't have to make your own routes and ask yourself if they are correct or not. Look into laravel official documentation for more info about this.
I would require a little help here if this method will work out well.
Firstly, I have a backend API server created using Laravel and Laravel Passport. Secondly, I have also created the frontend with Vuejs within the same project. As such, I will be required to use both the api.php and web.php routes. I am also redirecting these routes using vue-router.
Backend
Inside the web.php routes, I have used two different routes because I want to display generic contents on my landing site and the other as an authentication required dashboard.
Example:
web.php
As above, this is to capture the routes which are 404 Not Found that are directly manipulated in the address bar to redirect correctly to their respective pages. I also ended up having two different blade templates named as dashboard.blade.php and home.blade.php respectively. Is this an okay practice for a Laravel-Vuejs project? Or are there ways that you would like to recommend?
dashboard.blade.php
home.blade.php
Login related problem with login page using the layout of the landing page into another layout of the dashboard page
The problem that I am faced with when doing an API login with the password grant is that the login page does not redirect to the dashboard page properly. The URL route does change but the page is rendered as blank.
The login using axios here:
I have managed to fix the problem.
In web.php, since we have
Route::get('/dashboard/{any?}', function () {
return view('dashboard');
})->where('any', '^(?!.*api).*$[\/\w\.-]*');
Inside of my Login.vue redirect handler, I use location.href = '/dashboard' instead of this.$router.push('dashboard').
I am facing token mismatch issue in new server while working fine in localhost. I have tried every possible way to clear cache and give SESSION_DOMAIN path in env but all seems useless.
Also session cookies are not being created in web browser while creating in storage/framework/session folder.
Please help me !
Are you getting tokenMismatchException exception?
If yes, some of the possible reasons are:
Check your files for PHP end tag "?>", if exists remove it. For more detail refer to this link.
You may need to use web middleware. For more detail refer to this link (although it is about laravel 5.2 but, it may work for your situation too).
Another thing to try is checking for web middleware presence. Normally it should be automatically applied to routes/web.php
Route::group(['middleware' => ['web']], function () {
<routes>
});
Also check out https://laravel.com/docs/5.3/upgrade to see if you have any code that might have been influenced by this update.
And lastly, it would be nice if you could post a piece of code which is responsible for sessions in your app.
This might be a very silly question because I'm new to PHP frameworks. But I have got the concept of MVC framework. But this routing thing is very confusing me.
I created a controller and view for dashboard and set it as default. Then I wanted to check if the user is logged in then send him to the login page if it's not.
So, what I did in the routes is this:
$route['default_controller'] = 'dashboard/index';
$route['login'] = 'login';
But even if I add a thousand more controllers and routes, it still goes to the default i.e the dashboard. This was supposed to work http://localhost/codeigniter/login. I'm very sure I haven't put a redirect-to-dashboard code in the login page.
at first it's important to understand to routing in codeigniter.
The key for understanding is $route['url/path'] = controller/method (or "public function")
So. In your case, you routing to Login controller and index method (or index function...this it interpretation of codeigniter )
If you want to check if is user logged or not, you may try to check for examle session variable inside of called functon. But it depends on your application. But I think that is not good idea to check it in routes.php
Hope it helps you.
The default controller in routes, will be the controller that opens your index or first page.
If your first page is in controller "pages", then that is your default controller. Do not use it for your secure dashboard pages
Your first page should be the index method in the controller. It should look like this
$route['default_controller'] = "pages";
Where pages is your first page controller